Home > Enterprise >  Django context doesn't load after post
Django context doesn't load after post

Time:03-10

I'm trying to pass "message_name" to index.html through the form but the context does not render after submitting the form. It appears a blank page. I can see through the page inspector that the text was submitted but the content doesn't show.

I imagine this should be really easy but I could not find solution till now.

Thank you in advance for your help.

#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) 

#template ....

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

    <div >
      <div >
        <h3 >EMAIL</h3>
        <span >info.mail.com</span>
      </div>

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

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

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

CodePudding user response:

Change this:

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

...to this:

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

...if your intent is to pick up the context dictionary you have declared above your POST method.

If you want to add in additional context such as message_name, you could do something like:

    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...

...which you can do once you have declared a context dictionary somewhere up above, as you have.

One last word: You have a fair number of model queries going on in this view. Are any these models related via OneToOne, ForeignKey, or ManyToMany relations? If so, consider fewer queries with the select_related() and/or prefetch_related() methods for the sake of efficiency. More about those here and here in the Django docs. You can access or iterate through related objects in your template, for example:

{{queried_object.related_object.field_value}}
  • Related