2012-03-31 141 views
1

我有3个数据库模型 - 学期,科和Notecard更新单个的Django模型字段

的Notecard模型具有“已知”字段,我使用作为已知的Notecard对象分类为“桩”(1)或未知(0):

class Notecard(models.Model): 
    notecard_name = models.CharField(max_length=50) 
    notecard_body = models.TextField() 
    section = models.ForeignKey(Section) 
    known = models.BooleanField() 

我有两个视图 - known_list和unkown_list显示相应的桩(known_list下面供参考):

def known_list(request, section_name): 

    try: 
     section = Section.objects.get(section_name__iexact = section_name) 
    except Section.DoesNotExist: 
     raise Http404 

    known_list = Notecard.objects.filter(known=1, section=section) 
    paginator = Paginator(known_list, 1) 

    if known_list:  
     try: 
      page = int(request.GET.get('page', '1')) 
     except ValueError: 
      page = 1 

     try: 
      known = paginator.page(page) 
     except (EmptyPage, InvalidPage): 
      known = paginator.page(paginator.num_pages) 

     context = RequestContext(request) 
     return render_to_response('notecards/known.html', {"known": known}, context_instance=context) 
    else: 
     url = reverse('notecard_list', kwargs={'section_name': section_name}) 
     return HttpResponseRedirect(url) 

此视图带来在S ection_name从之前的视图中显示,以显示所有被点击的部分以及已知桩中的Notecard对象。

在下面的模板,你可以看到我的分页到notecards一个页面:

{% 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 %} 

urls.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"), 
) 

这么说,我想补充一个“发送到未知“按钮,这将允许用户将他们当前所在页面的记录卡发送到未知的桩上(只需将已知字段更改为= 0,从分页列表中移除记事卡,然后移至分页中的下一页) 。

我试过复制我的new_notecard视图,其中包含模型的完整形式,但我无法弄清楚如何更新单个字段。

我也尝试过使用queryset.update(),但无法弄清楚如何从特定的notecard捕获pk。

我一直试图弄清这一点在我自己一个多月,但我一直不成功。先谢谢你。

编辑:

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

回答

2

你必须创建一个特定视图与特定的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 %} 

您也可以通过notecard ID作为之后的参数。 next参数告诉改变后去哪里,在这里我选择已知列表的相同页面,因为一旦当前卡被移除,下一个卡在这个索引

+0

感谢您的回复。 2件事..在视图中,我不确定从哪里导入HttpRedirect。我用HttpResponseRedirect对它进行了测试,以防万一它是一个错误,但是我得到这个错误:模板的第一行

行中出现'错误的块标记:'反向',预期的'endblock'或'endblock content'。谢谢。 – 2012-03-31 15:05:57

+0

经过一番修补之后,我明白了。谢谢你的帮助! – 2012-03-31 22:52:41

+0

是的,在那里有一些错字,它是'HttpResponseRedirect'和'url'('reverse'是实现了'url'的python方法)纠正了答案:) – 2012-04-01 09:28:01

0

捕获特定notecard对象的pk可以通过为该notecard定义一个特定的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> 

你也可以定义与notecard ID /包被提交的隐藏字段,并更新到数据库的表单(当然,您将需要相应地更新您的视图功能)。

在本质上,更新特定notecard对象,您只需在您的视图功能做(表单提交或者,如果你愿意,在商家资讯页面的纯Ajax实现)这样

notecard.known = False 
notecard.save() 
+0

谢谢你的回应。我已经添加了我的urls.py,并且您可以看到每个notecard具有唯一的url,但在通过/ known/pile查看时没有。您正在说要将添加到/ known/url ..我仍然可以正确分页吗? – 2012-03-31 04:55:44

+0

如果您已经定义了'r'^ notecard /(?P [\ w | \ W] +)/'',那么确实如果您有逻辑确保您/您的用户不有重复的记录卡名称。想象一下,用户A创建一个名为'myfirstcard'的记事卡,用户B也创建一个名为'myfirstcard'的记录卡。您最终有两个同名的记事卡,因此在您尝试呈现notecard_detail页面时会导致错误。您首先需要通过使用id而不是名称或使用独特的slu in来代替您的名字来解决此问题。分页是分开的。 – 2012-03-31 05:10:40

+0

分页应该独立于您的notecard_detail实现。 – 2012-03-31 05:10:59

相关问题