为了账号安全,请及时绑定邮箱和手机立即绑定

python3.7加django2.1,http://127.0.0.1:8000/blog/edit/action/无法跳转到首页

def edit_page(request):
    return render(request,'blog/edit_page.html')

def edit_action(request):
    title = request.POST.get('title','TITLE')
    content = request.POST.get('content','CONTENT')
    # title = request.POST['title']
    # content = request.POST['content']
    models.Article.objects.create(title=title,content=content)
    # return HttpResponseRedirect(reversed('/blog/index/'))
    articles = models.Article.objects.all()
    return render(request, 'blog/index.html', {'articles': articles})


<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Edit Page</title>
</head>
<body>
<form action="/blog/edit/action/" method="post">
 {% csrf_token %}
    <label>Title
        <input type="text" name="Title">
    </label>
    <br/>
    <label>Content
        <input type="text" name="Content">
    </label>
    <br/>
    <input type="submit" value="Submit">
</form>
</body>
</html>
urlpatterns = [
    re_path('index/', views.index),
    re_path('article/(?P<article_id>[0-9]+)/', views.article_page,name = 'article_page'),
    # re_path('article/(?P<article_id>[0-9]+)/', views.article_page),
    re_path('edit/',views.edit_page,name = 'edit_page'),
    re_path('edit/action/',views.edit_action, name = 'edit_action'),
]



<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{#<h1>{{ article.title }}</h1>#}
{#<h3>{{ article.content }}</h3>#}
<h1>
    <a href="">新文章</a>
</h1>
{%  for article in articles %}
    <a href="{%url 'blog:article_page' article.id %}">{{ article.title }}</a>
{#    <a href="/blog/article/{{ article.id }}">{{ article.title }}</a>#}
 <br/>
{% endfor %}
</body>
</html>


https://img1.sycdn.imooc.com//5b90dd590001c85107480291.jpg

正在回答

4 回答

 File "D:\Program Files\Anaconda3\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func

    response = view_func(request, *args, **kwargs)

  File "E:\workspaces\MxOnline\extra_apps\xadmin\views\website.py", line 66, in get

    return login(request, **defaults)

TypeError: login() got an unexpected keyword argument 'extra_context'

[21/May/2019 15:44:13] "GET /xadmin/ HTTP/1.1" 500 89464

和你一样的问题 请问你解决了吗  怎么处理

0 回复 有任何疑惑可以回复我~

求问题xadmin后有遇到TypeError: login() got an unexpected keyword argument 'extra_context'这个问题吗,版本和您的一样

0 回复 有任何疑惑可以回复我~
edit_action 的路由应该这么写:
path('index/', views.edit_action, name='edit_action'),


0 回复 有任何疑惑可以回复我~
#1

学一点忘一点

我试了你的,虽然数据提交后可以跳转到首页,但是数据根本没有真正的提交到数据库中,所以首页也没有数据,只是跳转到首页去了,还是不行,按理来说写入数据后,最后应该跳转到首页去的,我去看看view.py中的逻辑,看看能否迁移到index去
2019-01-09 回复 有任何疑惑可以回复我~
应该改成re_path('edit_action/',views.edit_action, name = 'edit_action'),
还有<label>Title
        <input type="text" name="Title">
    </label>
    <br/>
    <label>Content
        <input type="text" name="Content">
        应该是<input type="text" name="title">
    </label>
    <br/>
    <label>Content
        <input type="text" name="content">
        这样就可以运行了


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
django入门与实践
  • 参与学习       65127    人
  • 解答问题       898    个

手把手带你进入Django开发的大门,充分领略Django的魅力

进入课程

python3.7加django2.1,http://127.0.0.1:8000/blog/edit/action/无法跳转到首页

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号