Python 更新单个Django模型字段

Python 更新单个Django模型字段,python,django,Python,Django,我有3个数据库模型-学期,部分和记事本 记事卡模型有一个“已知”字段,我使用该字段将记事卡对象分类为“堆”,即已知(1)或未知(0): 我有两个视图-已知列表和未知列表,显示相应的桩(以下已知列表供参考): 此视图从上一个视图中引入节_名称,以显示单击的节中以及已知堆中的所有记事卡对象 在下面的模板中,您可以看到我将记事卡分页为一页一张: {% extends "base.html" %} {% block content %} <h1 class='title'><

我有3个数据库模型-学期,部分和记事本

记事卡模型有一个“已知”字段,我使用该字段将记事卡对象分类为“堆”,即已知(1)或未知(0):

我有两个视图-已知列表和未知列表,显示相应的桩(以下已知列表供参考):

此视图从上一个视图中引入节_名称,以显示单击的节中以及已知堆中的所有记事卡对象

在下面的模板中,您可以看到我将记事卡分页为一页一张:

{% extends "base.html" %}

{% block content %}
    <h1 class='title'><a href="/">NoteCards!</a></h1>
    {% for notecard in known.object_list %}
        <h1 class='notecard'>{{ notecard.notecard_name }}</h1>
        <h3 class='notecard'>{{ notecard.notecard_body }}</h3>
    {% endfor %}
    <div class="pagination">
    <span class="step-links">
        {% if known.has_previous %}
            <a class="navlink" href="?page={{ known.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ known.number }} of {{ known.paginator.num_pages }}
        </span>

        {% if known.has_next %}
            <a class="navlink" href="?page={{ known.next_page_number }}">next</a>
        {% endif %}
    </span>
    </div>
{% endblock %}
{%extends“base.html”%}
{%block content%}
{known.object_list%%中记事卡的%
{{notecard.notecard_name}
{{notecard.notecard_body}
{%endfor%}
{如果已知,则为%has_previous%}
{%endif%}
第{{known.paginator.num_pages}页中的{{known.number}}
{%如果已知。有_next%}
{%endif%}
{%endblock%}
url.py

urlpatterns += patterns('',
    url(r'^(?P<section_name>[\w|\W]+)/unknown/$', unknown_list, name="unknown_list"),
    url(r'^(?P<section_name>[\w|\W]+)/known/', known_list, name="known_list"),
    url(r'^semester/(?P<semester_name>[\w|\W]+)/', section_list, name="section_list"),
    url(r'^section/(?P<section_name>[\w|\W]+)/', notecard_list, name="notecard_list"),
    url(r'^notecard/(?P<notecard_name>[\w|\W]+)/', notecard_detail, name="notecard_detail"),
    url(r'^$', semester_list, name="semester_list"),
)
urlpatterns+=模式(“”,
url(r'^(?P[\w |\w]+)/unknown/$,unknown_list,name=“unknown_list”),
url(r'^(?P[\w |\w]+)/known/',known_list,name=“known_list”),
url(r'^Semmer/(?P[\w |\w]+)/,节列表,name=“节列表”),
url(r'^section/(?P[\w |\w]+)/',记事卡列表,name=“记事卡列表”),
url(r“^notecard/(?P[\w |\w]+)/”,notecard\u detail,name=“notecard\u detail”),
url(r'^$',学期列表,name=“学期列表”),
)
也就是说,我想添加一个“发送到未知”按钮,允许用户将其当前所在页面的记事卡发送到未知堆(只需将已知字段更改为=0,从分页列表中删除记事卡,并移动到分页中的下一页)

我试着复制我的新的记事本视图,它包含了模型的完整形式,但我不知道如何更新单个字段

我也尝试过使用queryset.update(),但无法找出如何从特定的记事卡捕获pk

我一个多月来一直试图自己解决这个问题,但一直没有成功。先谢谢你

编辑:


好像我挂断的是在分页的每一页上拉记事卡的主键。例如,如果我在分页的第3页-当按下“发送到未知”按钮时,如何在视图中识别该记事卡并将其从已知(1)更新为未知(0)

可以通过为该记事卡定义特定url来捕获特定记事卡对象的主键。例如:-

# urls.py
url(r'^notecard/(?P<notecard_id>\d+)/$',
    'notecard',
    name='notecard'),

# corresponding views.py
def notecard(request, note_card_id):
    notecard = get_object_or_404(Notecard, pk=note_card_id)
    template = 'notecard/notecard.html'
    template_vars = {'notecard': notecard}
    render(request, template, template_vars)

# notecard/notecard.html
<h2>{{ notecard.notecard_name }}</h2>
<p>{{ notecard.notecard_body }}</p>

必须使用特定url创建特定视图才能处理此问题,例如:

# urls.py
url(r'^movetounknown/(?P<notecard_id>[\w|\W]+)/', notecard_move_to_unknown)

# views.py
@require_POST
def notecard_move_to_unknown(request, notecard_id):
    notecard = Notecard.objects.get(pk=notecard_id)
    notecard.known = False
    notecard.save()
    return HttpResponseRedirect(request.POST['next'])


# template
{% for notecard in known.object_list %}
    <h1 class='notecard'>{{ notecard.notecard_name }}</h1>
    <h3 class='notecard'>{{ notecard.notecard_body }}</h3>
    <form action="{% url views.move_to_unknown notecard.pk %}" method="post">
        <input type="hidden" name="next" value="{% url known_list known.section.section_name %}?page={{known.paginator.number}}"/>
        <input type="submit" value="Move to unknown list"/>
    </form>
{% endfor %}
#url.py
url(r'^movetounknown/(?P[\w |\w]+)/',记事卡(移动到未知位置)
#views.py
@需要职位
def记事卡移动到未知(请求,记事卡id):
notecard=notecard.objects.get(pk=notecard\u id)
notecard.known=False
notecard.save()
返回HttpResponseRedirect(request.POST['next'])
#模板
{known.object_list%%中记事卡的%
{{notecard.notecard_name}
{{notecard.notecard_body}
{%endfor%}
您还可以将notecard id作为post参数传递。
next
参数告诉我更改后要去哪里,这里我选择了已知列表的同一页,因为一旦当前卡被移除,下一张就在这个索引处

谢谢您的回复。我已经添加了my urls.py,您可以看到,当单独查看时,每个便笺卡都有唯一的url,但通过/known/pile查看时则没有。您的意思是将or添加到/known/url。。我仍然可以正确地分页吗?如果您已经定义了
r'^notecard/(?P[\w |\w]+)/”
,如果您有一个逻辑来确保您/您的用户没有重复的notecard名称,那就足够了。假设用户A创建了一张名为“myfirstcard”的记事卡,用户B也创建了一张名为“myfirstcard”的记事卡。您最终拥有两个同名的记事卡,因此在尝试呈现记事卡详细信息页面时将导致错误。您首先需要通过使用id而不是名称或使用唯一的slug来代替您的名称来解决此问题。分页是独立的。分页应该独立于notecard_详细信息实现。已知url和notecard_详细信息是两个独立的视图。已知的url就是有问题的url。也许我没有正确地表达这一点——我将在上午再次检查并尝试改写它。谢谢你的努力,谢谢你的回复。2件事。。在我看来,我不确定从哪里导入HttpRedirect。我用HttpResponseRedirect测试了它,以防它是一个打字错误,但是我得到了这个错误:
无效的块标记:“reverse”,应该是模板第一行的“endblock”或“endblock content”
。谢谢。经过一番修补,我终于找到了。谢谢你的帮助!是的,有一些输入错误,它是
HttpResponseRedirect
url
reverse
是实现
url
的python方法)更正了答案:)
# urls.py
url(r'^notecard/(?P<notecard_id>\d+)/$',
    'notecard',
    name='notecard'),

# corresponding views.py
def notecard(request, note_card_id):
    notecard = get_object_or_404(Notecard, pk=note_card_id)
    template = 'notecard/notecard.html'
    template_vars = {'notecard': notecard}
    render(request, template, template_vars)

# notecard/notecard.html
<h2>{{ notecard.notecard_name }}</h2>
<p>{{ notecard.notecard_body }}</p>
notecard.known = False
notecard.save()
# urls.py
url(r'^movetounknown/(?P<notecard_id>[\w|\W]+)/', notecard_move_to_unknown)

# views.py
@require_POST
def notecard_move_to_unknown(request, notecard_id):
    notecard = Notecard.objects.get(pk=notecard_id)
    notecard.known = False
    notecard.save()
    return HttpResponseRedirect(request.POST['next'])


# template
{% for notecard in known.object_list %}
    <h1 class='notecard'>{{ notecard.notecard_name }}</h1>
    <h3 class='notecard'>{{ notecard.notecard_body }}</h3>
    <form action="{% url views.move_to_unknown notecard.pk %}" method="post">
        <input type="hidden" name="next" value="{% url known_list known.section.section_name %}?page={{known.paginator.number}}"/>
        <input type="submit" value="Move to unknown list"/>
    </form>
{% endfor %}