试图让用户向页面添加新主题,但页面只返回项目符号,没有主题名称。在管理后台运行良好,但却会破坏purpos

2024-09-27 21:26:00 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的第一个python/Django项目中工作。需要让用户向博客页面添加新主题,但页面只返回项目符号,没有主题名称。在管理后端工作正常,这当然会破坏用户输入的目的。”


    {% extends 'blog/base.html' %}

    {% block content %}
      <p>Add a new topic:</p>
       <form action="{% url 'blog:new_topic' %}" method='post'>
         {% csrf_token %}
         {{ form.as_p }}
         <button name='submit'>add topic</button>
       </form>

    {% endblock content %}
    ```

    ```
    {% extends 'blog/base.html' %}

    {% block content %}

      <p>BlogPosts</p>

      <ul>
        {% for topic in blogposts %}
           <li>
              <a href="{% url 'blog:topic' topic.id %}">{{ topic }}</a>
           </li>
        {% empty %}
           <li>Nothing's yet been posted</li>
        {% endfor %}
       </ul>

       <a href="{% url 'blog:new_topic' %}">Add a new topic:</a>

    {% endblock content %}
    ````
    {% extends 'blog/base.html' %}

    {% block content %}

      <p>Topic: {{ topic }}</p>

      <p>Entries:</p>

      <ul>
      {% for entry in entries %}
        <li>
          <p>{{ entry.date_added|date:'M d, Y H:i' }}</p>
          <p>{{ entry.text|linebreaks }}</p>
        </li>
      {% empty %}
        <li>
          There are no entries for this post yet.
        </li>
      {% endfor %}
      </ul>

    {% endblock content %}
    ````
    Here are the views: 
        from django.shortcuts import render

        # Create your views here.
        from django.http import HttpResponseRedirect
        from django.urls import reverse

        from .models import BlogPost
        from .forms import BlogPostForm

        def index(request):
            """The home page for Road Ahead project."""
            return render(request, 'blog/index.html')

        def blogposts(request):
            """Show all blogposts (in reverse order)."""
            blogposts = BlogPost.objects.order_by('-date_added')
            context = {'blogposts': blogposts}
            return render(request, 'blog/blogposts.html', context)

        def topic(request, topic_id):
            """Show a single post and all its entries."""
            topic = BlogPost.objects.get(id=topic_id)
            entries = topic.entry_set.order_by('date_added')
            context = {'topic': topic, 'entries': entries}
            return render(request, 'blog/topic.html', context)

        def new_topic(request):
            """Add a new topic."""
            if request.method != 'POST':
                # No data submitted; create a new blank form.
                form = BlogPostForm()
            else:
                # Post data submitted; process data.
                form = BlogPostForm(data=request.POST)
                if form.is_valid():
                    form.save()
                    return HttpResponseRedirect(reverse('blog:blogposts'))

            context = {'form': form}
            return render(request, 'blog/new_topic.html', context)

    ````

    {% extends 'blog/base.html' %}

    {% block content %}

      <p>Topic: {{ topic }}</p>

      <p>Entries:</p>

      <ul>
      {% for entry in entries %}
        <li>
          <p>{{ entry.date_added|date:'M d, Y H:i' }}</p>
          <p>{{ entry.text|linebreaks }}</p>
        </li>
      {% empty %}
        <li>
          There are no entries for this post yet.
        </li>
      {% endfor %}
      </ul>

    {% endblock content %}

除了index和base=parent html页面之外,我还有另外3个页面全部复制到上面:blogposts,它列出了所有带项目符号的blog主题,topic,它显示主题名称和条目,new\u topic,它应该允许用户主题条目。新的主题似乎是这里的问题-我可以输入一个新的主题,我没有得到任何错误消息来排除故障,但我得到的只是要点,没有主题名称。同样,在后端添加主题和内容也是可能的;但不是我想要的。谢谢


Tags: form主题newfordatetopicrequesthtml

热门问题