15
Why does after logging in, it redirects to accounts/profile/? Is there
a way to change that? Preferably after successfully logging in I would
like Django to redirect back to the page before the login page.
Just change setting LOGIN_REDIRECT_URL
If I were to create my own view and template for accounts/profile/,
then where should I put it? Django’s built-in users (auth_user) is
shared among all Django apps inside a project, so should I place the
view.py in the project folder and not inside the app folder?
I like to create an app called “project_specific” in every project. That’s where I put all the stuff that’s not meant to be reusable and that couples many apps.
You can also create a views.py at the project level, but that is sort of messy compared to making a project specific app.
In reality it does not matter where you put it.
Or does Django profile actually takes care of this whole
account/profiles/ thing? I already extended Django’s User class with
my own UserProfile, but it’s more like additional fields to the User
table than an actual “profile” (I didn’t create avatars or anything
like that, just simple stuff like addresses and phone numbers, but
most importantly, some custom user types that my app depends on).
That’s not the way to add extra user fields. I recommend that you read the docs on Storing additional information about users.
10
For a minimal approach that doesn’t require a standalone app,
-
Create a template and call it
profile.html
or anything you want.<p>This is your profile, {{ user.username }}.</p>
-
In
urls.py
, add a url pattern that points to your profile template, mark itlogin_required
, and give the url a name:# ... from django.views.generic import TemplateView from django.contrib.auth.decorators import login_required urlpatterns = [ # ... url(r'^accounts/profile/$', login_required(TemplateView.as_view(template_name='profile.html')), name='user_profile'), # ... ]
-
In
settings.py
, add the following line:LOGIN_REDIRECT_URL = 'user_profile'
This line tells Django to perform a reverse URL lookup by name when redirecting a user after a login. Without this line, your app will still work but it will be fragile because it relies on an arbitrary hard-coded URL that is implicitly configured by Django. With this line, if you or someone else decides that user profiles should be at /me/
, you could change the URL in step 2 without breaking your app.
- [Django]-How to pass multiple values for a single URL parameter?
- [Django]-How to make an auto-filled and auto-incrementing field in django admin
- [Django]-Ignoring Django Migrations in pyproject.toml file for Black formatter
2
- Set LOGIN_REDIRECT_URL in settings – https://docs.djangoproject.com/en/dev/ref/settings/#login-redirect-url
- Create account app, where contains code for this.
You may use django userena for full-stack user area: https://django-userena.readthedocs.org/en/latest/
- [Django]-How do I deploy Django on AWS?
- [Django]-Django 1.7 – App 'your_app_name' does not have migrations
- [Django]-Filtering dropdown values in django admin