Django - how to implement an example.com/username url system

前端 未结 3 657
离开以前
离开以前 2020-12-15 01:56

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 t

相关标签:
3条回答
  • 2020-12-15 02:49

    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):
        ...
    
    0 讨论(0)
  • 2020-12-15 02:55

    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?

    0 讨论(0)
  • 2020-12-15 03:01

    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}}

    0 讨论(0)
提交回复
热议问题