post 后 Django 上下文未加载

Django context doesn't load after post

我试图通过表单将“message_name”传递给 index.html,但提交表单后上下文没有呈现。出现一个空白页。通过page inspector我可以看到提交了文本但是没有显示内容

我想这应该很容易,但直到现在我都找不到解决方案。

预先感谢您的帮助。

#views.py

from django.shortcuts import render

from .models import Home, About, Profile, Category, Skills, Item, Audiogil, ImageSkills
from django.core.paginator import Paginator


def index(request):
    # Home
    home = Home.objects.latest('updated')

    # About
    about = About.objects.latest('updated')
    #profiles = Profile.objects.filter(about=about)
    profiles = Profile.objects.all()

    # Skills
    categories = Category.objects.all()

    skillsimage = ImageSkills.objects.latest('updated')

    # Videos
    videos = Item.objects.all()

    # Setup video pagination
    p = Paginator(Item.objects.all(), 2)
    page = request.GET.get('page')
    video_list = p.get_page(page)

    # Audios
    audios = Audiogil.objects.all()

    
    context = {
        'home': home,
        'about': about,
        'profiles': profiles,
        'categories': categories,
        'videos': videos,
        'video_list': video_list,
        'audios': audios,
        'skillsimage': skillsimage,
    }

    if request.method == "POST":
        message_name = request.POST['message-name']
        message_email = request.POST['message-email']
        message = request.POST['message']

        return render(request, 'index.html', {'message_name':message_name})


    
    return render(request, 'index.html', context) 

#模板 ....

  <!--===== CONTACT =====-->
  <section class="contact section" id="contact">
    <h2 class="section-title">Contacto - {{ message_name }}</h2>

    <div class="contact__container bd-grid">
      <div class="contact__info">
        <h3 class="contact__subtitle">EMAIL</h3>
        <span class="contact__text">info.mail.com</span>
      </div>

      <form action="{% url 'index' %}" class="contact__form" method="post">
        {% csrf_token %}
        <div class="contact__inputs">
          <input type="text" placeholder="Nome" name="message-name" class="contact__input" />
          <input type="mail" placeholder="Email" name="message-email" class="contact__input" />
        </div>

        <textarea
          name="message"
          id=""
          cols="0"
          rows="10"
          class="contact__input"
        ></textarea>

        <input type="submit" value="Enviar" class="contact__button" />
      </form>
    </div>
  </section>
</main>

改变这个:

return render(request, 'index.html', {'message_name':message_name})

...为此:

return render(request, 'index.html', context)

...如果您的目的是获取您在 POST 方法上方声明的上下文字典。

如果您想添加额外的上下文,例如 message_name,您可以这样做:

    if request.method == "POST":
        context['message_name'] = request.POST['message-name']
        message_email = request.POST['message-email']
        message = request.POST['message']
        # seems like you're missing (or not showing) more logic here...

...一旦您在上面某处声明了上下文字典,您就可以这样做。

最后一句话:您在此视图中进行了相当多的模型查询。这些模型是否通过 OneToOne、ForeignKey 或 ManyToMany 关系关联?如果是这样,为了效率起见,考虑使用select_related() and/or prefetch_related() 方法进行更少的查询。有关 Django 文档中 here and here 的更多信息。您可以访问或遍历模板中的相关对象,例如:

{{queried_object.related_object.field_value}}