Where is a good place to work on accounts/profile in Django with the Django registration app?
Asked Answered
S

3

21

I've noticed that after I log in with Django registration it redirects me to accounts/profile/. By default Django registration's url.py doesn't handle accounts/profile/, so I need to create my own.

Actually this questions is three-fold:

  1. 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.
  2. 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?
  3. 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).
Salmagundi answered 23/3, 2012 at 7:59 Comment(0)
M
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.

Maley answered 23/3, 2012 at 8:23 Comment(0)
H
10

For a minimal approach that doesn't require a standalone app,

  1. Create a template and call it profile.html or anything you want.

     <p>This is your profile, {{ user.username }}.</p>
    
  2. In urls.py, add a url pattern that points to your profile template, mark it login_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'),
         # ...
     ]
    
  3. 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.

Haehaecceity answered 9/5, 2016 at 3:10 Comment(1)
I just noticed that I’m importing login_required but wasn’t using it. It needs to be applied otherwise the profile page can be visited without login (although it’ll probably not show much since there won’t be a user). I fixed it now but didn’t test it.Haehaecceity
C
2
  1. Set LOGIN_REDIRECT_URL in settings - https://docs.djangoproject.com/en/dev/ref/settings/#login-redirect-url
  2. 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/

Coletta answered 23/3, 2012 at 13:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.