Django - how to implement an example.com/username

2019-01-23 00:03发布

问题:

I am trying to implement on my website a simple and friendly address system.

What i'm thinking about is when the user logged in, his username will be displayed in the address bar.

  • www.example.com/username1 (for home page profile)
  • www.example.com/username1/about/
  • www.example.com/username1/gallery/

  • www.example.com/username2 (for home page profile)
  • www.example.com/username2/about/
  • www.example.com/username2/gallery/

And additionally if anyone enter the address www.example.com/username1, will be shown the profile of user1.

I already implemented a register/Login system using Django-Allauth

mySite/urls.py

url(r'^accounts/', include('allauth.urls')),

home/urls.py

url(r'^$', views.index),

home/views.py

def index(request):

return render_to_response('home/index.html', context_instance=RequestContext(request))

I tried to follow some examples like Facing problem with user profile url scheme like example.com/username in django

But i dont have this thing working yet. I dont understand what to do :(

Please, give some advise.

回答1:

Add the following url as the last item of the mySite/urls.py:

urlpatterns = patterns('',
    ...
    url(r'^(?P<username>\w+)/', include('userapp.urls')),
)

Then the username parameter will be passed to the views of your userapp:

userapp/urls.py:

from userapp import views

urlpatterns = patterns('',
    url(r'^$', views.profile, name='user_profile'),
    url(r'^about/$', views.about, name='user_about'),
    url(r'^gallery/$', views.gallery, name='user_gallery'),
)

userapp/views.py:

def profile(request, username):
    user = get_object_or_404(User, username=username)
    return render(request, 'userapp/profile.html', {'profile_user': user})

def about(request, username):
    ...

def gallery(request, username):
    ...


回答2:

It's almost perfect, but i have a bug. Let me explain. When i login (my login system is: django-allauth) on the http://127.0.0.1:8000/accounts/login/ i return to http://127.0.0.1:8000 with an error 404.

I am not getting the http://127.0.0.1:8000/username/ with the template, when the login botton is clicked.

The instalation of django-allauth require adding to the settings.py

LOGIN_REDIRECT_URL = '/'

How can i redirect to http://127.0.0.1:8000/username/ and show the correct template?



回答3:

1. Regarding

How can i redirect to

Based on the answer from https://stackoverflow.com/a/20143515/4992248

# settings.py:
ACCOUNT_ADAPTER = 'project.your_app.allauth.AccountAdapter'

# project/your_app/allauth.py:
from allauth.account.adapter import DefaultAccountAdapter

class AccountAdapter(DefaultAccountAdapter):

  def get_login_redirect_url(self, request):
      return 'request.user.username'  # probably also needs to add slash(s)

Would be better to use get_absolute_url, ie return 'request.user.get_absolute_url'. In this case you need to do:

# 1. Add `namespace` to `yoursite/urls.py`
urlpatterns = patterns('',
    ...
    url(r'^(?P<username>\w+)/', include('userapp.urls', namespace='profiles_username')),
)

# 2. Add the code below to the Users class in models.py
def get_absolute_url(self):
    # 'user_profile' is from the code shown by catavaran above
    return reverse('profiles_username:user_profile', args=[self.username])

2. Regarding

show the correct template

catavaran wrote correct urls which leads to views.profile, so in view.py you need to write:

from django.shortcuts import render

from .models import UserProfile  # import model, where username field exists
from .forms import UserProfileForm  # import Users form

def profiles(request, username):
    user = get_object_or_404(UserProfile, username=username)

    return render(request, 'home/profiles.html', {'user_profile_form': user})

In template (i.e. profiles.html) you can show user's data via {{user_profile_form.as_p}}