I'm trying to learn Django and am trying to setup a user login system. My site only shows the base.html contents, and not the contents of login.html. If I remove the extends base.html, the login form seems to show up correctly. Am I doing something wrong?
login.html
{% extends "base.html" %}
{% load url from future %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
{% endblock %}
base.html
<body>
<div>
{{ user }}
{% if user.is_anonymous %}
login
{% else %}
logout
{% endif %}
</div>
Your base.html template is missing a corresponding {% block content %}{% endblock content %} and also a closing </body> tag. There's nowhere for the content in login.html to go.
Brandon is right.
you can think {%block content %} as a hole in your base.html and the extended file only replace/fill in that hole.
Related
Django 2.2
I have a list view controlled by admin.py class. No custom template, all default. I can control what fields from the table should be shown in the view with this:
fields = ('myfield1','myfield2', ...).
Each row in the list table has a checkbox in the first column, the source looks like this:
<td class="action-checkbox">
<input type="checkbox" name="_selected_action" value="123" class="action-select">
</td>
My questions are:
How to disable those checkboxes (preferably, from Django code, without introducing a custom template) ?
Can it be done for SOME of the checkboxes (let's say I have a list of pk ids for the rows I don't want to see checkboxes.)
You can delete Items with those CheckBoxes, but if you want to customize your own admin page to override it
You can use this doc https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#admin-overriding-templates
This question is 2 years old now, but for the case someone still needs it, the following code works to overwrite the change-list_results.html:
{% load i18n static %}
{% if result_hidden_fields %}
<div class="hiddenfields">{# DIV for HTML validation #}
{% for item in result_hidden_fields %}{{ item }}{% endfor %}
</div>
{% endif %}
{% if results %}
<div class="results">
<table id="result_list">
<thead>
<tr>
{% for header in result_headers %}
{% if "checkbox" in header.text %}
{% else %}
<th scope="col" {{ header.class_attrib }}>
{% if header.sortable %}
{% if header.sort_priority > 0 %}
<div class="sortoptions">
<a class="sortremove" href="{{ header.url_remove }}" title="{% translate "Remove from sorting" %}"></a>
{% if num_sorted_fields > 1 %}<span class="sortpriority" title="{% blocktranslate with priority_number=header.sort_priority %}Sorting priority: {{ priority_number }}{% endblocktranslate %}">{{ header.sort_priority }}</span>{% endif %}
</div>
{% endif %}
{% endif %}
<div class="text">{% if header.sortable %}{{ header.text|capfirst }}{% else %}<span>{{ header.text|capfirst }}</span>{% endif %}</div>
<div class="clear"></div>
{% endif %}
</th>{% endfor %}
</tr>
</thead>
<tbody>
{% for result in results %}
{% if result.form and result.form.non_field_errors %}
<tr><td colspan="{{ result|length }}">{{ result.form.non_field_errors }}</td></tr>
{% endif %}
<tr>
{% for item in result %}
{% if "_selected_action" in item %}
{% else %}
{{ item }}
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}
It stops stops the output of the for loops if theres a checkbox in there. --> It just removes the checkboxes.
Im a newbie in Django and I'm using Django-Cms. I'm trying to set a login page.
I have created this login.html template:
{% extends "base.html" %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form method="post" action="{% url django.contrib.auth.views.login %}">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
{% endblock %}
My base.html is:
{% load cms_tags sekizai_tags %}
<html>
<head>
{% render_block "css" %}
</head>
<body>
{% cms_toolbar %}
{% placeholder base_content %}
{% block base_content %}{% endblock %}
{% render_block "js" %}
</body>
</html>
My url is:
url(r'^accounts/', include('django.contrib.auth.urls')),
url(r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'registration/login.html'}),
And when I go to /accounts/login I don't see anything. Just the django-cms toolbar. Why?
Thanks for reading
You probably wanted to use {% block base_content %} instead {% block content %} in your login.html template as you have {% block base_content %} in your base.html template.
I want to wrap a in a label, but when the template was rendered, it didn't generate the right html, here is my code:
{% for item in studentinfo %}
<form action="" method="">
{% csrf_token %}
<tr>
<td>{{ item.nickname|default_if_none:"" }}</td>
<td>{{ item.mobile|default_if_none:"" }}</td>
<td>{{ item.register_at|date:"Y-m-d"|default_if_none:"" }}</td>
<td>{{ item.sales.first_name|default_if_none:"" }}</td>
<td><strong class="red-text">{{ item.price|default_if_none:"" }}</strong></td>
<td><strong class="red-text">{{ item.remaining|default_if_none:"" }}</strong></td>
<td>{{ item.level|default_if_none:"" }}</td>
<td>
{% if item.state %}
{{ setstudentform.state|default:item.state }}
{% else %}
{{ setstudentform.state }}
{% endif %}
</td>
<td>{{ item.source|default_if_none:"" }}</td>
<td>
{% if item.feature %}
{{ setstudentform.feature|default:item.feature }}
{% else %}
{{ setstudentform.feature }}
{% endif %}
</td>
</tr>
</form>
{% endfor %}
but it's generated html was like this:
<form method="" action=""></form>
<input type="hidden" value="8N1O4Oks4MmgN1ujanMZX0o2X5XGUMny" name="csrfmiddlewaretoken">
with nothing inside the <form>, why is that
You want this:
<form method="POST">
{% csrf_token %}
<table>
{% for item in studentinfo %}
<tr>
<td>...</td>
</tr>
{% endfor %}
</table>
</form>
The way you template is structured is not valid HTML.
Also, you don't need this:
{% if item.feature %}
{{ setstudentform.feature|default:item.feature }}
{% else %}
{{ setstudentform.feature }}
{% endif %}
Simply {{ setstudentform.feature|default:item.feature }} will do what you intend.
The question is incomplete, I think -- we'd need to see what's around that {% for %} too, but I'll hazard a guess.
It looks like you're trying to interleave forms between table rows, which isn't likely legal in HTML and will cause the parsed DOM tree to look possibly something like your "generated HTML" example.
Did you check the HTML on the wire (using View Source (aside from IE, which when I last used it always output the parsed DOM tree as "source"), not a DOM inspector)? My educated guess is you'll find your HTML correct (or well, at least as you've templated it :) ) there.
So, in short, I don't think
<table>
<form><tr>...</tr></form>
<form><tr>...</tr></form>
</table>
isn't legal HTML.
In a Django project, my homepage view is as follows:
#login_required
def home(request):
return render(request, 'home.html')
So that when someone tries to access the homepage, they're automatically taken to a login form if no one is logged in. Here's that form, straight from the Django docs:
{% extends "base.html" %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form method="post" action="{% url django.contrib.auth.views.login %}">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
{% endblock %}
Once someone logs into the form, they get taken to the homepage which looks like this:
{% extends "base.html" %}
{% block content %}
<form method="post" action="{% url django.contrib.auth.views.logout_then_login %}">
{% csrf_token %}
<input type="submit" value="logout" />
</form>
<h1>Home Page</h1>
{% endblock %}
As you can see, I've attempted to create a logout button that will take the user right back to the login page. However, I noticed that when first arriving at the login page, the URL ends in ?next=/, whereas once I "logout", the "login" page I'm taken to has a URL lacking ?next=/. And when I try to login using that page, I'm sent to the URL /accounts/profile/ (instead of the proper homepage URL), which doesn't exist. I'm guessing I've done something wrong in urls.py, but I'm not sure what:
(r'^accounts/login/$', 'django.contrib.auth.views.login'),
(r'^accounts/logout/$', 'django.contrib.auth.views.logout_then_login'),
What am I missing here?
You should tell Django where it should redirect visitors when it doesn't receive a next parameter. This is done with a LOGIN_REDIRECT_URL settings, as explained in the Django documentation.
I am New beginner in python and django...
i want to know how can i create a login form by using django forms(forms.py)
In your urls.py file link to the built in Django login view, and pass in the path to a template you wish to use as the login page:
(r'^login/$', 'django.contrib.auth.views.login', {
'template_name': 'myapp/login.html'
}),
And here is an example of what the template my look like (from the Django docs):
{% extends "mybase.html" %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
{% endblock %}
Yes, you can. Actually, you don't need to create your own form. Simply use auth module and create your own login template. Read this: http://docs.djangoproject.com/en/dev/topics/auth/