Reputation: 1617
why this context not rendering in my html template:
return render(request, 'index.html',{'message_name':name})
This context will print user name after successful submit my contact form. here is my code:
views.py
@csrf_exempt
def home_view(request,*args,**kwargs):
name = None
if request.method == "POST":
contact_form = ContactForm(request.POST)
if contact_form.is_valid():
name = request.POST['name']
email = request.POST['email']
subject = request.POST['subject']
message = request.POST['message']
save_details = Contact(name=name,email=email,subject=subject,message=message)
save_details.save()
return HttpResponseRedirect("http://127.0.0.1:8000/")
return render(request, 'index.html',{'message_name':name})
else:
print("not submitted")
else:
contact_form = ContactForm()
return render(request, 'index.html',{'form':contact_form})
app urls.py
from django.urls import path
from pages import views
urlpatterns = [
path('', views.home_view, name="home"),
]
root urls.py
from django.contrib import admin
from django.urls import path,include
from pages import urls
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls')),
]
index.html
<!--===== CONTACT =====-->
<section class="contact section" id="contact">
<h2 class="section-title">Contact</h2>
{% if message_name %}
<div class="centerTest">
<h1> Thanks {{ message_name }} for your message. We will get back to you very soon</h1>
</div>
{% else %}
<div class="contact__container bd-grid">
<form action="#contact" method = "POST" class="contact__form">
{% for error in form.non_field_errors %}
<div class="alert alert-danger" role="alert">
{{ error }}
</div>
{% endfor %}
<label>Name:</label>
{{ form.errors.name }}
<input type="text" placeholder="Name" name="name" class="contact__input" {% if form.is_bound %}value="{{ form.name.value }} {% endif %}">
<label>Email:</label>
{{ form.errors.email }}
<input type="mail" placeholder="Email" name="email" class="contact__input" {% if form.is_bound %}value="{{ form.email.value }} {% endif %}">
<label>Subject:</label>
{{ form.errors.subject }}
<input type="text" placeholder="optional" name="subject" class="contact__input" {% if form.is_bound %}value="{{ form.subject.value }} {% endif %}">
<label>Message:</label>
{{ form.errors.message }}
<textarea name="message" placeholder="message" id="" cols="0" rows="10" class="contact__input" >{% if form.is_bound %}{{ form.message.value }} {% endif %}</textarea>
<input type="submit" value="Send" class="contact__button button">
{% endif %}
if I remove the HttpResponseRedirect then it showing context in my html template
But the problem is it resubmitting my from again and again on refresh. After adding HttpResponseRedirect it stop resubmitting the form but not printing the context.
Upvotes: 0
Views: 1655
Reputation: 192
You can't pass context directly in redirect or HttpResponseRedirect in django. You need to be use session method. Try this:
@csrf_exempt
def home_view(request,*args,**kwargs):
name = None
if request.method == "POST":
contact_form = ContactForm(request.POST)
if contact_form.is_valid():
name = request.POST['name']
email = request.POST['email']
subject = request.POST['subject']
message = request.POST['message']
save_details = Contact(name=name,email=email,subject=subject,message=message)
save_details.save()
request.session['name'] = name
return redirect(home_view)
else:
print("not submitted")
else:
contact_form = ContactForm()
return render(request, 'index.html',{'form':contact_form})
Pass this {{ request.session.name }}
in your index.html for show your context.
Upvotes: 1
Reputation: 1709
As a basic Python concept (and probably any other programming language), you cannot have two return
sentences, one above the other in a function as the first one will make the second one unreachable.
Example:
def say_hello(name, age):
return f"hello {name}"
return f"your age is {age}"
If you execute this function, you will only get "hello john doe" as the second return statement is never executed.
In your case, if you keep the return HttpResponseRedirect()
, the return render()
will never be executed, therefore you will never see the context.
If you want to redirect the user to a specific route you should do it conditionally in order to have the chance to execute one return
or the other.
Upvotes: 1