Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django EditView中出现意外错误_Python_Django - Fatal编程技术网

Python Django EditView中出现意外错误

Python Django EditView中出现意外错误,python,django,Python,Django,当我点击url时,我发现我的所有字段都是空的,而不是填充的值,但是当我尝试从django admin编辑它们时,它们工作正常。我可以在那里看到我的所有值,并且可以更改它们的值 models.py forms.py url.py index.html {%csrf_令牌%} {{form.as_p}} 当我尝试打印每个步骤时,如果form.is\u有效,则此步骤不显示任何输出 虽然在控制台中工作正常,但没有出现任何错误。请按如下所示重新构造if块: if request.method == '

当我点击url时,我发现我的所有字段都是空的,而不是填充的值,但是当我尝试从django admin编辑它们时,它们工作正常。我可以在那里看到我的所有值,并且可以更改它们的值

models.py forms.py url.py index.html

{%csrf_令牌%}
{{form.as_p}}
当我尝试打印每个步骤时,如果form.is\u有效,则此步骤
不显示任何输出


虽然在控制台中工作正常,但没有出现任何错误。请按如下所示重新构造if块:

if request.method == 'POST':
    print(form)
    if form.is_valid():
        form.save()
else:
    form = PostForm()
return render(request, 'index.html',{'form':form})

我已经解决了。实际上,为了进行编辑,我需要从实例中获取值。但我的GET表单中填充了request.POST,这意味着它将表单视为“创建新表单”。所以,我删除了request.POST并只保留instance=query,这就是我如何启用GET请求从实例获取数据的方法

def index(request,pk):
    if request.user.is_superuser:
        try:
            query = Post.objects.get(id=pk)

        except:
            raise Http404('0')
        form = PostForm(request.POST or None, instance=query)

        if request.method == 'POST':

            if form.is_valid():
                form.save()

            return redirect('all')
        else:
            form = PostForm(instance=query)
        return render(request, 'index.html',{'form':form})

感谢@AbijithMg的评论。。。但这就是我们创建post的方式,它只是创建一个表单来填充它。它将创建一个新表单,而不是打开现有表单。嗯,你的问题不清楚。您应该在GET请求中提到我需要填充值!
urlpatterns = [
    path('<int:pk>/edit/',views.index),
]
def index(request,pk):
    if request.user.is_superuser:
        try:
            query = Post.objects.get(id=pk)
            print(query)
        except:
            raise Http404('0')
        form = PostForm(request.POST or None, instance=query)
        print(form)
        if request.method == 'POST':
            print(form)
            if form.is_valid():
                form.save()

            return redirect('all')
        else:
            form = PostForm(request.POST, instance=query)
        return render(request, 'index.html',{'form':form})
<form method="post">
    {% csrf_token %}
    {{form.as_p}}
    <input type="submit" value="submit">
</form>
if request.method == 'POST':
    print(form)
    if form.is_valid():
        form.save()
else:
    form = PostForm()
return render(request, 'index.html',{'form':form})
def index(request,pk):
    if request.user.is_superuser:
        try:
            query = Post.objects.get(id=pk)

        except:
            raise Http404('0')
        form = PostForm(request.POST or None, instance=query)

        if request.method == 'POST':

            if form.is_valid():
                form.save()

            return redirect('all')
        else:
            form = PostForm(instance=query)
        return render(request, 'index.html',{'form':form})