如何在Django url中使用slug,如mysite.com/user

tvz2xvvm  于 2022-10-22  发布在  Go
关注(0)|答案(1)|浏览(185)

我想在用户登录后创建一个类似website.com/user的目录!
我不确定app/url中的url pattern是哪个。py应该调用这个myfun来捕获url中的slug:user

def myfun(request, user):
    user = get_user_model().objects.get(slug=user)
    return render(request, 'authorization/home.html', {'user' : user})

型号.py

class TwitterUser(models.Model):
    screen_name = models.CharField(max_length=255)
    name = models.CharField(max_length=255)
    user = models.SlugField('auth.User', unique=True, null=True)

应用程序/urls.py

urlpatterns = [
    path('', views.index, name='index'),
    path('twitter_login/', views.twitter_login, name='twitter_login'),
    path('twitter_callback/', views.twitter_callback, name='twitter_callback'),
    path('twitter_logout/', views.twitter_logout, name='twitter_logout'),
]

视图.py

def twitter_login(request):
    twitter_api = TwitterAPI()
    url, oauth_token, oauth_token_secret = twitter_api.twitter_login()
    if url is None or url == '':
        messages.add_message(request, messages.ERROR, 'Unable to login. Please try again.')
        return render(request, 'authorization/error_page.html')
    else:
        twitter_auth_token = TwitterAuthToken.objects.filter(oauth_token=oauth_token).first()
        if twitter_auth_token is None:
            twitter_auth_token = TwitterAuthToken(oauth_token=oauth_token, oauth_token_secret=oauth_token_secret)
            twitter_auth_token.save()
        else:
            twitter_auth_token.oauth_token_secret = oauth_token_secret
            twitter_auth_token.save()
        return redirect(url)

def twitter_callback(request):
    if 'denied' in request.GET:
        messages.add_message(request, messages.ERROR, 'Unable to login or login canceled. Please try again.')
        return render(request, 'authorization/error_page.html')
    twitter_api = TwitterAPI()
    oauth_verifier = request.GET.get('oauth_verifier')
    oauth_token = request.GET.get('oauth_token')
    twitter_auth_token = TwitterAuthToken.objects.filter(oauth_token=oauth_token).first()
    if twitter_auth_token is not None:
        access_token, access_token_secret = twitter_api.twitter_callback(oauth_verifier, oauth_token, twitter_auth_token.oauth_token_secret)
        if access_token is not None and access_token_secret is not None:
            twitter_auth_token.oauth_token = access_token
            twitter_auth_token.oauth_token_secret = access_token_secret
            twitter_auth_token.save()
            # Create user
            info = twitter_api.get_me(access_token, access_token_secret)
            if info is not None:
                twitter_user_new = TwitterUser(twitter_id=info[0]['id'], screen_name=info[0]['username'],
                                               name=info[0]['name'], profile_image_url=info[0]['profile_image_url'])
                twitter_user_new.twitter_oauth_token = twitter_auth_token
                user, twitter_user = create_update_user_from_twitter(twitter_user_new)
                if user is not None:
                    login(request, user)
                    return redirect('index')
            else:
                messages.add_message(request, messages.ERROR, 'Unable to get profile details. Please try again.')
                return render(request, 'authorization/error_page.html')
        else:
            messages.add_message(request, messages.ERROR, 'Unable to get access token. Please try again.')
            return render(request, 'authorization/error_page.html')
    else:
        messages.add_message(request, messages.ERROR, 'Unable to retrieve access token. Please try again.')
        return render(request, 'authorization/error_page.html')

@login_required
@twitter_login_required
def index(request):
    return render(request, 'authorization/home.html')

@login_required
def twitter_logout(request):
    logout(request)
    return redirect('index')

非常感谢您的回复:)

wn9m85ua

wn9m85ua1#

将其添加到您的URL中。py与相关占位符,并在视图中获取占位符作为参数:

urlpatterns = [
    path('', views.index, name='index'),
    path('<str:user>', views.myfun, name='myfun')
]

您可以在Django文档https://docs.djangoproject.com/en/4.1/topics/http/urls/中查看更多信息

相关问题