Django - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
getlost
V2EX    Django

Django

  •  
  •   getlost 2018-08-09 00:12:32 +08:00 3208 次点击
    这是一创建于 2670 天前的主题,其中的信息可能已经有所发展或是发生改变。

    跟着《 python 编程从入门到实践》练习 Django,遇到如下问题:创建了一个 Topic 表单,有一个 Entry 表单用外键链接到 Topic,在试图函数里定义了修改 Entry 的函数如下: def edit_entry(request, entry_id):

    """修改条目""" entry = Entry.objects.get(id=entry_id) topic = entry.topic if request.method != 'POST': form = EntryForm(instance=entry) else: form = EntryForm(instance=entry, data=request.POST.get('fields')) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('xs:topic', args=[topic.id])) cOntext= {'topic': topic, 'form': form, 'entry': entry} return render(request, 'xs/edit_entry.html', context) 

    现在我想修改 Topic,跟着上面的函数定义了下面这个函数:

    def edit_topic(request, topic_id):

    """修改主题""" topic = Topic.objects.get(id=topic_id) if request.method != 'POST': form = TopicForm(instance=topic) else: form = TopicForm(instance=topic, data=request.POST.get('fields')) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('xs:topics')) cOntext= {'topic': topic, 'form': form} return render(request, 'xs/edit_topic.html', context) 

    这就出问题了,第二个 if 语句(if form.is_valid())似乎没有执行,导致没有跳转至 topics.html 页面

    4 条回复    2018-08-09 11:17:53 +08:00
    helloworld12
        1
    helloworld12  
       2018-08-09 01:17:42 +08:00
    多 print 打印 日志
    georgema1982
        2
    georgema1982  
       2018-08-09 07:54:15 +08:00
    我目测不是没执行,而是 form.is_valid()是 False,所以它又回到了 edit_topic.html
    getlost
        3
    getlost  
    OP
       2018-08-09 11:16:56 +08:00
    @helloworld12 谢谢指教,form.is_valid()是 false
    getlost
        4
    getlost  
    OP
       2018-08-09 11:17:53 +08:00
    @georgema1982 确实是这样,难道不允许修改嘛?
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1062 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 18:04 PVG 02:04 LAX 10:04 JFK 13:04
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86