【问题标题】:ValueError in Django Python Forum applicationDjango Python 论坛应用程序中的 ValueError
【发布时间】:2020-02-09 14:02:10
【问题描述】:

我正在尝试创建一个表单,用户可以在其中添加一个类别中的新主题。然后用户将能够对每个主题发表评论。一切正常,直到我尝试为 new_topic 添加表单页面

我收到的错误:

/new_topic/ 处的值错误 视图 speak_space.views.new_topic 没有返回 HttpResponse 对象。它返回 None 。

这是我的 view.py 和 urls.py 文件的代码:

view.py

from django.shortcuts import render, redirect

from .models import Category
from .models import Topic
from .forms import TopicForm

def index(request):
    """The home page for speak_space"""
    return render(request, 'speak_space/index.html')

def categories(request):
    """Show all categories."""
    categories = Category.objects.order_by('date_added')
    context = {'categories': categories}
    return render(request, 'speak_space/categories.html', context)

def category(request, category_id):
    """Show a single category(tribe) and all of its topics(convos)."""
    category = Category.objects.get(id=category_id)
    topics = category.topic_set.order_by('-date_added')
    context = {'category': category, 'topics': topics}
    return render(request, 'speak_space/category.html', context)

def topics(request):
    """Show all topics within a category(tribe)."""
    topics = Topic.objects.order_by('date_added')
    context = {'topics': topics}
    return render(request, 'speak_space/topics.html', context)

def topic(request, topic_id):
    """Show a single topic(convo/post) and all of it's replies."""
    topic = Topic.objects.get(id=topic_id)
    entries = topic.entry_set.order_by('-date_added')
    context = {'topic': topic, 'entries': entries}
    return render(request, 'speak_space/topic.html', context)

def new_topic(request):
    """Add a new conversation topic."""
    if request.method != 'POST':
        # No data submitted; create a blank form.
        form = TopicForm()
    else:
        # POST data submitted; process data.
        form = TopicForm(data=request.POST)
        if form.is_valid():
            form.save()
            return redirect('speak_space:topics')

        # Display a blank or invalid form.
        context = {'form': form}
        return render(request, 'speak_space/new_topic.html', context)

这是我的 url 文件:

from django.urls import path

from . import views

app_name = 'speak_space'
urlpatterns = [
    # Home page
    path('', views.index, name='index'),

    # Page that shows all categories
    path('categories/', views.categories, name='categories'),

    # Profile page for a category
    path('categories/<int:category_id>/', views.category, name='category'),

    # Page that shows all topics.
    path('topics/', views.topics, name='topics'),

    # Detail page for a single topic(conversation)
    # Where you go after you click on someones post
    path('topics/<int:topic_id>/', views.topic, name='topic'),

    # Page for adding a new conversation topic
    path('new_topic/', views.new_topic, name='new_topic'),
 ]

和我的表单页面:

from django import forms

from .models import Topic

class TopicForm(forms.ModelForm):
    class Meta:
        model = Topic
        fields = ['text']
        labels = {'text': ''}

【问题讨论】:

    标签: python django cascade forum valueerror


    【解决方案1】:

    您的new_topic 视图在 GET 请求的情况下不会返回任何内容。你应该让return 声明通用:

    def new_topic(request):
        """Add a new conversation topic."""
        if request.method != 'POST':
            # No data submitted; create a blank form.
            form = TopicForm()
        else:
            # POST data submitted; process data.
            form = TopicForm(data=request.POST)
            if form.is_valid():
                form.save()
                return redirect('speak_space:topics')
    
        # The changes are here, now return will work for all requests types POST and GET
        context = {'form': form}
        return render(request, 'speak_space/new_topic.html', context)
    

    【讨论】:

      【解决方案2】:

      以下行缩进

      # Display a blank or invalid form.
      context = {'form': form}
      return render(request, 'speak_space/new_topic.html', context)
      

      在您的 new_topic 方法中。

      【讨论】:

        猜你喜欢
        • 2014-02-18
        • 1970-01-01
        • 2012-05-22
        • 2015-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多