表单提交按钮不起作用并获取URL重定向错误

时间:2016-12-22 04:15:57

标签: python django heroku

我在部署中遇到两个表单/重定向问题(使用Django)可能是相关的:

  1. 我有一个表单来创建新的博客帖子,在点击提交时,应该根据中选择的类别将您重定向到网址。在生产中它工作正常,但在部署中单击提交按钮创建一个新帖子什么也不做(没有重定向,表单保持原样,没有任何东西添加到网站的其他地方)并且我的错误日志上没有弹出任何内容。请注意,我没有为" action ="设置值。在表单模板中,我不确定应该包括哪些(如果有的话)仍然允许我根据表单中的值重定向到不同的页面。

  2. 我还可以编辑一篇博客文章,将我带到填写了所有字段的表单页面。当我编辑一个值并点击提交时,它会更新博客文章,但会将我重定向到页面错误:Not Found: The requested URL /resources/books.views.resources was not found on this server.如果您查看了urls.py文件,则没有" books.views.resources& #34; url在哪里。书籍是我命名的模型。我不确定为什么在部署中这会被添加。

  3. 真的很感激任何想法。这是相关代码:

    post_edit.html

    {% block content %}
    <div id="content">
        <div class="padding">
            <h1>New post</h1>
                <form method="POST" class="progresstracker-form">{% csrf_token %}
                {{ form.as_p }}
                <button type="submit" class="save btn btn-default">Save</button>
    
                </form>
    
        </div>
    </div>
    {% endblock %}
    

    urls.py

    urlpatterns = [
        url(r'^admin/', include(admin.site.urls)),
        url(r'^accounts/login/$', django.contrib.auth.views.login, name='login'),
        url(r'^account/logout$', django.contrib.auth.views.logout, name='logout', kwargs={'next_page': '/'}),
        url(r'^blogtopics/computer-science/$', views.compsci, name='computer-science'),
        url(r'^blogtopics/data-science/$', views.datasci, name='data-science'),
        url(r'^blogtopics/other/$', views.other, name='other'),
        url(r'^blogtopics/$', views.blogtopics, name='blogtopics'),
        url(r'^resources/$', views.resources, name='resources'),
        url(r'^resources/edit/$', views.resources_post_edit, name='resources_post_edit'),
        url(r'^new/$', views.post_new, name='post_new'),
        url(r'^progresstracker/$', views.progresstracker, name='progresstracker'),
        url(r'^blogtopics/(?P<category>[\w-]+)/(?P<slug>[\w-]+)/$', views.bt_detail, name='bt_detail'),
        url(r'^(?P<category>[\w-]+)/(?P<slug>[\w-]+)/$', views.pt_detail, name='pt_detail'),
        url(r'^blogtopics/(?P<category>[\w-]+)/(?P<slug>[\w-]+)/edit/$', views.post_edit, name='bt_post_edit'),
        url(r'^(?P<category>[\w-]+)/(?P<slug>[\w-]+)/edit/$', views.post_edit, name='pt_post_edit'),
        url(r'^drafts/$', views.post_draft_list, name='post_draft_list'),
        url(r'^blogtopics/(?P<category>[\w-]+)/(?P<slug>[\w-]+)/remove/$', views.post_remove, name='bt_post_remove'),
        url(r'^(?P<category>[\w-]+)/(?P<slug>[\w-]+)/remove/$', views.post_remove, name='pt_post_remove'),
        url(r'^blogtopics/(?P<category>[\w-]+)/(?P<slug>[\w-]+)/publish/$', views.post_publish, name='bt_post_publish'),
        url(r'^(?P<category>[\w-]+)/(?P<slug>[\w-]+)/publish/$', views.post_publish, name='pt_post_publish'),
    ]
    

    views.py

    def resources(request):
        post = get_object_or_404(Post, slug='resources')
        return render(request, 'blog/resources.html', {'post': post})
    
    @login_required 
    def post_new(request):
        if request.method == "POST":
            form = PostForm(request.POST)
            if form.is_valid():
                post = form.save(commit=False)
                post.author = request.user
                post.save()
                if post.category == 'progresstracker':
                    return redirect('books.views.pt_detail', slug=post.slug, category=post.category)
                elif post.category == 'resources':
                    return redirect('books.views.resources')
                else:
                    return redirect('books.views.bt_detail', slug=post.slug, category=post.category)
        else:
            form = PostForm()
        return render(request, 'blog/post_edit.html', {'form': form})
    
    @login_required     
    def post_edit(request, slug, category):
        post = get_object_or_404(Post, slug=slug, category__slug=category)
        if request.method == "POST":
            form = PostForm(request.POST, instance=post)
            if form.is_valid():
                post = form.save(commit=False)
                post.author = request.user
                post.save()
                if post.category == 'progresstracker':
                    return redirect('books.views.pt_detail', slug=post.slug, category=post.category)
                elif post.category == 'resources':
                    return redirect('books.views.resources')
                else:
                    return redirect('books.views.bt_detail', slug=post.slug, category=post.category)
    
        else:
            form = PostForm(instance=post)
        return render(request, 'blog/post_edit.html', {'form': form})
    
    @login_required  
    def resources_post_edit(request):
        post = get_object_or_404(Post, slug='resources')
        if request.method == "POST":
            form = PostForm(request.POST, instance=post)
            if form.is_valid():
                post = form.save(commit=False)
                post.author = request.user
                post.save()
                return redirect('books.views.resources')
        else:
            form = PostForm(instance=post)
        return render(request, 'blog/post_edit.html', {'form': form})
    

1 个答案:

答案 0 :(得分:1)

对于#1,请检查处理提交的表单的位置 - 哪个视图,并检查那里发生的事情。

对于#2, 在视图中的redirect()函数调用中,使用urls.py中定义的视图名称而不是视图调用等python。

例如,你有

return redirect('books.views.bt_detail', slug=post.slug, category=post.category)

所以改为使用

return redirect('bt_detail', slug=post.slug, category=post.category)
#---------------^