170👍
You do not need to make an extra view for this, the functionality is already built in.
First each page with a login link needs to know the current path, and the easiest way is to add the request context preprosessor to settings.py (the 4 first are default), then the request object will be available in each request:
settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.request",
)
Then add in the template you want the Login link:
base.html:
<a href="{% url django.contrib.auth.views.login %}?next={{request.path}}">Login</a>
This will add a GET argument to the login page that points back to the current page.
The login template can then be as simple as this:
registration/login.html:
{% block content %}
<form method="post" action="">
{{form.as_p}}
<input type="submit" value="Login">
</form>
{% endblock %}
33👍
To support full urls with param/values you’d need:
?next={{ request.get_full_path|urlencode }}
instead of just:
?next={{ request.path }}
- [Django]-You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application
- [Django]-Django logging of custom management commands
- [Django]-Trying to migrate in Django 1.9 — strange SQL error "django.db.utils.OperationalError: near ")": syntax error"
30👍
This may not be a “best practice”, but I’ve successfully used this before:
return HttpResponseRedirect(request.META.get('HTTP_REFERER','/'))
- [Django]-Best way to integrate SqlAlchemy into a Django project
- [Django]-Django can' t load Module 'debug_toolbar': No module named 'debug_toolbar'
- [Django]-Iterate over model instance field names and values in template
14👍
Django’s built-in authentication works the way you want.
Their login pages include a next
query string which is the page to return to after login.
Look at http://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.decorators.login_required
- [Django]-Django: Open uploaded file while still in memory; In the Form Clean method?
- [Django]-What's the difference between select_related and prefetch_related in Django ORM?
- [Django]-Django: Display Choice Value
1👍
I linked to the login form by passing the current page as a GET parameter and then used ‘next’ to redirect to that page. Thanks!
- [Django]-How do I do an OR filter in a Django query?
- [Django]-Django dynamic forms – on-the-fly field population?
- [Django]-How to completely dump the data for Django-CMS
1👍
I encountered the same problem. This solution allows me to keep using the generic login view:
urlpatterns += patterns('django.views.generic.simple',
(r'^accounts/profile/$', 'redirect_to', {'url': 'generic_account_url'}),
)
- [Django]-Add a custom button to a Django application's admin page
- [Django]-How to understand lazy function in Django utils functional module
- [Django]-How to pass multiple values for a single URL parameter?
1👍
In registration/login.html
(nested within templates
folder) if you insert the following line, the page will render like Django’s original admin login page:
{% include "admin/login.html" %}
Note: The file should contain above lines only.
- [Django]-How does one make logging color in Django/Google App Engine?
- [Django]-Django Sitemaps and "normal" views
- [Django]-Iterate over model instance field names and values in template
-1👍
See django docs for views.login(), you supply a ‘next’ value (as a hidden field) on the input form to redirect to after a successful login.
- [Django]-Django: Open uploaded file while still in memory; In the Form Clean method?
- [Django]-Altering one query parameter in a url (Django)
- [Django]-Pylint "unresolved import" error in Visual Studio Code
- [Django]-Why is logged_out.html not overriding in django registration?
- [Django]-Creating a JSON response using Django and Python
- [Django]-Django: manage.py does not print stack trace for errors