i've been wondering about how i could render a template just by passing a made-inside-view string so that i woudn't have the need to create an html file. I read the Django Docs, but i couldn't find an explanation on this, as far as seen all was about giving the template path string, (also i tried a few lines, but got nothing).
Please, have this as an example:
from django.shortcuts import render
def my_view(request):
arbitrary_string_as_template = """
<form action="" method="POST">
{% csrf_token %}
<label for="">Username</label>
<input type="text" name="username">
<label for="">Password</label>
<input type="password" name="password">
<button type="submit">
submit
</button>
</form>
"""
return render(request, arbitrary_string_as_template, {})
So if this can be done... then i think it would have in its way a good potential, since it gains in terms of versatility..and btw..
#I'm New to Django
Thanks for your attention
You can construct a Template object [Django-doc] with the template string, and then .render(…) [Django-doc] it:
from django.http import HttpResponse
from django.template import Template, RequestContext
def my_view(request):
arbitrary_string_as_template = """
<form action="" method="POST">
{% csrf_token %}
<label for="">Username</label>
<input type="text" name="username">
<label for="">Password</label>
<input type="password" name="password">
<button type="submit">
submit
</button>
</form>
"""
template = Template(arbitrary_string_as_template)
context = RequestContext(request)
return HttpResponse(template.render(context))
If however the content is static, I would advise to use a file. It makes it easier to separate concerns, and write clean files that each focus on a specific part.
Related
I would like to call a function which is in /inscription/views.py since all views (because it's for the login). And I need to pass the username and the password in parameters to log the user.
def login_user(request):
if request.method =='POST':
auth_form=AuthenticationForm(data=request.POST)
if auth_form.is_valid():
username = request.POST.get('username')
password = request.POST.get('password')
uti = authenticate(username = username,password = password)
if uti:
if uti.is_active:
login(request, uti)
return HttpResponseRedirect('/accueil')
else:
return HttpResponse("Your account is disabled.")
else:
return HttpResponse("Invalid login details supplied.")
else:
auth_form=AuthenticationForm()
return render_to_response('authentication.html',
{'auth_form': auth_form}, RequestContext(request))
def logout_user(request):
logout(request)
And In my base.html I would like to add something like :
<label class="form_login">pseudo : </label>
<input type="text" name="username" id="id_username" class="login_input">
<label class="form_login">mot de passe : </label>
<input type="text" name="password" id="id_password" class="login_input">
<input value="login" type="submit"/>
<button>logout</button>
If I understand your question correctly, what you need is to force the user to login if he is not already logged in before he can access your views. To do this, all you need to do is to decorate your views with login_required decorator
from django.contrib.auth.decorators import login_required
#login_required
def my_view(request):
...
From the docs:
login_required() does the following:
- If the user isn’t logged in, redirect to settings.LOGIN_URL, passing
the current absolute path in the query string. Example:
/accounts/login/?next=/polls/3/.
- If the user is logged in, execute the view normally. The view code is
free to assume the user is logged in.
Update:
From your comment, now I understand that you need to make a form in all pages for the user to login, or a logout link if he is already logged in. First you need to define your URLs for these views:
url(r'^login/$', 'inscription.views.login', name='auth_login'),
url(r'^logout/$', 'inscription.views.logout', name='auth_logout'),
And in your base.html:
{% if user.is_authenticated %}
Logout
{% else %}
<form method="post" action="{% url 'auth_login' %}">
{% csrf_token %}
<input type="text" name="username" id="id_username">
<input type="text" name="password" id="id_password">
<input type="submit" value="Log in" />
</form>
{% endif %}
As a side note, I highly recommend you to use one of these reusable apps for auth and registration. unless you have strange requirements.
http://django-registration-redux.readthedocs.org/en/latest/
http://django-allauth.readthedocs.org/en/latest/
The problem which you are facing is , that u want the login and logout to work from other pages also, So, for this you need not to go for any extra function. All you need to do is, u just extend your base.html to all other html pages. Then you will surely be able to login and logout from all the pages.
Suppose you have login/logout in base.html
<label class="form_login">pseudo :</label>
<input type="text" name="username" id="id_username" class="login_input">
<label class="form_login">mot de passe : </label>
<input type="text" name="password" id="id_password" class="login_input">
<input value="login" type="submit"/>
<button>logout</button>
Now make some other html say test.html
There at the beginning you write
{% extends 'base.html' %}
followed by your HTML markup.
Don't forget to use
{% block content %} {% endblock %} **template tags**
In base as well as other HTML pages.
In other pages u try to write the complete code in template tags.
For query https://docs.djangoproject.com/en/1.7/topics/templates/
Also try using the concept of decorator.
How to avoid producing html files with content like this:
<form action="/contact/" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
Can I define somehow it in views or forms, so that there is no need for creating this files in templates?
Most class-based views use the TemplateResponseMixin. You can override the render_to_response method to return a HttpResponse object (or any subclass) that doesn't render a template.
class SimpleResponseMixin(object):
def render_to_response(self, context, **kwargs):
return HttpResponse(content='<content>', content_type=kwargs.get('content_type', self.content_type))
My login works fine except for not showing all errors. When i type an invalid username or password, these errors don't show and the page just refreshes without putting any errors down.
However when i leave one field blank, it shows the correct error:
So the missing errors are(from source):
error_messages = {
'invalid_login': _("Please enter a correct %(username)s and password. "
"Note that both fields may be case-sensitive."),
'inactive': _("This account is inactive."),
}
my code:
def login_user(request):
"""Logs a user into the application."""
auth_form = AuthenticationForm(None, request.POST or None)
# The form itself handles authentication and checking to make sure password and such are supplied.
if auth_form.is_valid():
(request, auth_form.get_user())
return HttpResponseRedirect(reverse('index'))
return render(request, 'login.html', {'auth_form': auth_form})
My template:
<form action="{% url 'login_user' %}" method="post" class="login">{% csrf_token %}
<div>
<input name="username" placeholder="Username:" type="text" name="username" value="" id="username" class="login">
{{ auth_form.username.errors }}
</div>
<div>
<input name="password" placeholder="Password:" type="password" name="password" value="" id="password" class="login">
{{ auth_form.password.errors }}
</div>
<div>
<center>
<a href="{% url 'register_user' %}">
register
</a>
<button type="submit" class="link">
login
</button>
</center>
</div>
</form>
what do i do wrong?
You aren't including form.non_field_errors in your template. See the docs on customizing the form template for an example.
As an aside, the AuthenticationForm takes the request as its first argument. It looks slightly strange that you are passing None instead of request.
In my template I have :
{% for link in solution_list %}
<form id="marked_solved_form{{link.id}}" action="" method="post">
{{mark_solved_form.is_solution}}
{% csrf_token %}
<label for="id_is_solution{{link.id}}">test form</label>
<input type="hidden" name="next" value="{{ request.path }}"/>
</form>
{% endfor %}
is_solution is just a booleanfield for a checkbox that user's can set.
The problem I am facing is that all the forms populate correctly with id's like id="marked_solved_form3" except for the {{mark_solved_form.is_solution}}. I thought I could just go to the modelform and edit the attributes:
class PostMarkedSolved(ModelForm):
class Meta:
model = MarkedSolved
def __init__(self, *args, **kwargs):
super(PostMarkedSolved, self).__init__(*args, **kwargs)
#self.fields['is_solution'].queryset = Factory.objects.all()
self.fields['is_solution'].widget.attrs={'onchange':'this.form.submit();', 'class':'testing{{link.id}}', 'id':'testing{{link.id}}'}
So far so good. Except when the form loads, I get the following for every checkbox in every list form:
<input name="is_solution" type="checkbox" class="testing{{link.id}}" onchange="this.form.submit();" id="testing{{link.id}}">
It's as though django is ignoring the modelform is escaping template tag {{link.id}} all together. Ideally it would return for each form instance something like:
<td>
<form id="marked_solved_form3" action="" method="post">
<input name="is_solution" type="checkbox" class="testing3" onclick="this.form.submit();" id="testing3" />
<div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='bwuXYhSpQA8yEOEPSaCt3wAANC0kR7CE' /></div>
<label for="id_is_solution3" onclick="this.form.submit();">test form</label>
<input type="hidden" name="next" value="/task/1"/>
</form>
</td>
<td>
<form id="marked_solved_form4" action="" method="post">
<input name="is_solution" type="checkbox" class="testing4" onclick="this.form.submit();" id="testing4" />
<div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='bwuXYhSpQA8yEOEPSaCt3wAANC0kR7CE' /></div>
<label for="id_is_solution4" onclick="this.form.submit();">test form</label>
<input type="hidden" name="next" value="/task/1"/>
</form>
</td>
I even went as far as to install django-widget-tweaks (Which is awesome, btw!), and tried doing:
{{mark_solved_form.is_solution|attr:"id:id_is_solution{{link.id}}"}}
However that choked because it cannot handle {{link.id}} within another template tag. :( Any suggestions would be appreciated. Thanks in advance!
The template isn't able to render those variables placed in the __init__ method. You should be able to access the model instance through self.instance and add the correct id's in the __init__ method by doing doing the following:
def __init__(self, *args, **kwargs):
super(PostMarkedSolved, self).__init__(*args, **kwargs)
self.fields['is_solution'].widget.attrs={
'onchange':'this.form.submit();',
'class':'testing{0}', 'id':'testing{1}'.format(self.instance.id, self.instance.id)
}
Hidden in Django's docs:
Also, a model form instance bound to a model object will contain a self.instance attribute that gives model form methods access to that specific model instance.
This is the wrong approach. You should use separate forms with prefixes, or even better use a formset.
Is there a way to identifying the validation error is associated with which form for a template contaning multiple forms?
Let me explain the scenario. I have login window popup script within my base.html
<div id="dialog1" class="window">
<form method="post" action="/accounts/login/?next=/IW/home" id='login-form' name=login-form>{% csrf_token %}
<div class="d-header">
{{ form.non_field_errors }}
<input type="text" name="username" id="id_username" value="" onclick="this.value=''"/><br/>
<input type="password" name="password" id="id_password" value="" onclick="this.value=''"/><br/>
<input type="hidden" name="login_form" value="1" />
<input type="submit" value="login" />
</div>
{% endif %}
</div>
</form>
</div>
<div id="mask"></div>
{% if form.non_field_errors %}
<script>
var maskHeight = $(document).height();
var maskWidth = $(window).width();
//Set heigth and width to mask to fill up the whole screen
$('#mask').css({'width':maskWidth,'height':maskHeight});
$('#mask').show();$('.window').show();
</script>
{% endif %}
As all other templates extends base,html whenever there is an error non_field error then login window pops up . I would like to show the login error only when login form is submit not on submit of someother form with a non field error.
For this I need to identify the name of the form.something like this {% ifequal form.form_name login_form %} - Display login error .Is this possible??
They isn't anything special about the name 'form' in the template. It's just a default context name, you can choose to name your forms anything you like.
Just name the forms differently in your context:
from django.http import Http404
def detail(request, poll_id):
# NOTE: This view code isn't really form processing code, just using it to show
# how you can pass different names into your template context.
login_form = MyLoginForm()
detail_form = MyDetailForm()
try:
p = Poll.objects.get(pk=poll_id)
except Poll.DoesNotExist:
raise Http404
return render_to_response('polls/detail.html', {'poll': p, 'login_form': login_form, 'detail_form': detail_form})
And then:
<div id="dialog1" class="window">
<form method="post" action="/accounts/login/?next=/IW/home" id='login-form' name=login-form>
{% csrf_token %}
<div class="d-header">
{{ login_form.non_field_errors }}
.
.
.
Also, if you want to do multiple instances of the same form type, have a look at formsets