【问题标题】:Django POST issues in views视图中的 Django POST 问题
【发布时间】:2014-07-22 04:26:38
【问题描述】:

我正在尝试允许用户更新模型的布尔变量,但我似乎无法将更改保存到数据库然后将更改呈现到模板。模型为“Pic”,布尔变量为“Good”。

在模板中(命名空间'single_picture'):

<form action="" method="POST">
    {% csrf_token %}
    {% if Pic.Good %}
    <input type="submit" name="choice" id="{{ Pic.id }}" value="False" />
    {% else %}
    <input type="submit" name="choice" id="{{ Pic.id }}" value="True" />
    {% endif %}
</form>

在应用程序的网址中:

url(r'^(?P<Pic_id>\d+)/$', views.single_picture, name='single_picture'),

在views.py中:

def single_picture(request, Pic_id):
# 'detail' in tutorial
if request.method == 'GET':
    pic = get_object_or_404(Pic, pk=Pic_id)
    latest_pictures_list = Pic.objects.all()
    return render(request, 'pictures/single_picture.html', {'Pic': pic, 'latest_pictures_list': latest_pictures_list})
elif request.method == 'POST':
    pic = get_object_or_404(Pic, pk=Pic_id)
    latest_pictures_list = Pic.objects.all()
    try:
        pic.Good=request.POST['choice']
    except (KeyError, Pic.DoesNotExist):
        return render(request, 'pictures/single_picture.html', {'Pic': pic, 'error_message': 'uhhhh...',
        })
    else:
        pic.save()
        return HttpResponseRedirect(reverse('pictures:pic', pic.id))

如果我在 try: 语句结束时将 pic.Good 打印到终端,我可以看到它显示了更改后的语句,但 pic.save() 似乎没有将其保存到数据库,我也得到了

在 /pictures/4/ 处配置不正确

包含的 urlconf 4 中没有任何模式

在渲染语句中。这似乎表明项目或应用程序的网址中缺少某些内容。如何正确保存更改并呈现更改?

【问题讨论】:

    标签: django python-2.7 django-views


    【解决方案1】:

    根据djangodocumentation on reverse function,第二个参数是urlconf参数。

    urlconf 参数是包含用于反转的 url 模式的 URLconf 模块。

    要为 url 构造提供参数,您需要使用 argskwargs 命名参数。

    尝试将您的 return 语句更改为以下内容:

    return HttpResponseRedirect(reverse('pictures:pic', args=[pic.id]))
    

    我不知道数据库保存问题,但尝试将您的图片保存调用移近图片更新代码以进行调试:

    elif request.method == 'POST':
        pic = get_object_or_404(Pic, pk=Pic_id)
        latest_pictures_list = Pic.objects.all()
        try:
            pic.Good=request.POST['choice']
            pic.save()
            return HttpResponseRedirect(reverse('pictures:pic', args=[pic.id]))
        except (KeyError, Pic.DoesNotExist):
            return render(request, 'pictures/single_picture.html', {'Pic': pic,     'error_message': 'uhhhh...',
            })
    

    编辑 1

    关于 NoReverseMatch 错误,您使用的是url namespaces ('pictures:pic')。我对它不是很熟悉,但我猜 reverse 函数在 pictures 的 url 配置中查找带有 name='pic' 的视图em> 应用程序。

    您确定这样的视图存在于您的 url 配置文件中吗?

    【讨论】:

    • 这似乎不能解决我的任何一个问题。更改仍未保存到数据库中,并且我在 /pictures/4/ Reverse 处获得 NoReverseMatch,用于 'pic' 参数为 '(4,)' 和关键字参数 '{}' 未找到。尝试了 0 种模式:[] 调用 reverse() 错误。
    • 能否请您发布您的应用程序的所有 url 配置?或者至少配置 'pictures:pic' url?
    • 看来你是对的。我将返回请求更新为:return HttpResponseRedirect(reverse('pictures:single_picture', args=[Pic_id]))'single_picture' 是 url 名称。我还必须将 arg 更新为“Pic_id”。
    【解决方案2】:

    不是你的问题的答案,但一个很好的提示是替换这个:

    <form action="" method="POST">
        {% csrf_token %}
        {% if Pic.Good %}
        <input type="submit" name="choice" id="{{ Pic.id }}" value="False" />
        {% else %}
        <input type="submit" name="choice" id="{{ Pic.id }}" value="True" />
        {% endif %}
    </form>
    

    用这个:

    <form action="" method="POST">
        {% csrf_token %}
        <input type="submit" name="choice" id="{{ Pic.id }}" value={% if Pic.Good %}"True"{% else %} "False" {% endif %} />
    </form>
    

    【讨论】:

      猜你喜欢
      • 2016-12-21
      • 2011-10-24
      • 2016-12-11
      • 2019-04-19
      • 2016-02-20
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 2015-06-02
      相关资源
      最近更新 更多