Reputation:
When i am running my Django project on local server. It is returning whole html code on webpage.
Like after executing command python manage.py runserver
and copy and pasting url on browser i am getting whole HTML file code instead element i have used.
My Html file
{% extends "wfhApp/base.html" %}
{% block body_block %}
<div class="jumbotron">
{% if registered %}
<h1>Thank you for registration</h1>
{% else %}
<h1>Register here!</h1>
<h3>Fill out the form:</h3>
<form enctype="multipart/form-data" method="post">
{% csrf_token %}
{{ user_form.as_p }}
<input type="submit" name="" value="Register">
</form>
{% endif %}
</div>
{% endblock %}
My views.py
from django.shortcuts import render
from wfhApp.forms import UserForm
def register(request):
registered = False
if request.method == 'POST':
user_form = UserForm(data = request.POST)
if user_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
registered = True
else:
print(user_form.errors)
else:
user_form = UserForm
return render(request, 'wfhApp/registration.html',
{'user_form': user_form},
{'registered': registered})
Above is from template inheritance.
Upvotes: 0
Views: 640
Reputation: 77912
Not 101% sure since you didn't really explained what you meant by "It is returning whole html code on webpage" but here:
return render(request, 'wfhApp/registration.html',
{'user_form': user_form},
{'registered': registered})
you're not correctly passing the template context - or, more exactly, you are passing {'user_form': user_form}
as the context and {'registered': registered}
as the response's content_type (which would else be the default "text/html").
What you want is (splitted on two lines for readability):
context = {
'user_form': user_form,
'registered': registered
}
return render(request, 'wfhApp/registration.html', context)
Upvotes: 1