简体   繁体   中英

Django form not visible after redirect

I'm trying to make django form to appear after user is logged in and authenticated but the form just does not appear on the page here is my code. I'm stuck on this for few days now and any help would be huge help

Views.py form code

def prasymas(request):
  context = initialize_context(request)
  user = context['user']

  form = Prasymas(request.POST or None)
  if form.is_valid():
    form.save()
  context ={
    'form':form
  }
  return HttpResponseRedirect(reverse('home'))

urls.py code

from django.urls import path
from . import views

urlpatterns = [
  # /
  path('', views.home, name='home'),
  # TEMPORARY
  path('signin', views.sign_in, name='signin'),
  path('signout', views.sign_out, name='signout'),
  path('callback', views.callback, name='callback'),
  path('prasymas', views.home, name='prasymas'),
 ]

template.py code

  {% extends "loginas/layout.html" %}
{% block content %}
<div class="container">
  <h1 class="d-flex justify-content-center"></h1>
  <p class="d-flex justify-content-center"></p>
  {% if user.is_authenticated %}
    <h4>Sveiki {{ user.firstname }} {{user.surname}}     
    </h4>
    <form method="POST">
      {% csrf_token %}
      {{ form.as_p }}
      <button class="btn btn-secondary">POST</button>
    </form>
  {% else %}<div class="d-flex justify-content-center">
    <a href="{% url 'signin' %}" class="btn btn-primary btn-large ">Prisijungti</a>
  </div>
  {% endif %}
 
</div>
{% endblock %}

From your urls.py it looks like the prasymas function in views.py is not being called.

Try making the following change to your urls.py file to include a call to the prasymas function.

Note: the line path('prasymas', views.prasymas, name='prasymas'), now points to views.prasymas not home. This means the path /prasymas will now render your form.

from django.urls import path
from . import views

urlpatterns = [
  # /
  path('', views.home, name='home'),
  # TEMPORARY
  path('signin', views.sign_in, name='signin'),
  path('signout', views.sign_out, name='signout'),
  path('callback', views.callback, name='callback'),
  path('prasymas', views.prasymas, name='prasymas'),
 ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM