Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 如何为每个用户分配一个特定链接,通过该链接我可以访问他的个人资料_Django_Django Models_Django Views_Django Templates - Fatal编程技术网

Django 如何为每个用户分配一个特定链接,通过该链接我可以访问他的个人资料

Django 如何为每个用户分配一个特定链接,通过该链接我可以访问他的个人资料,django,django-models,django-views,django-templates,Django,Django Models,Django Views,Django Templates,我想为每个用户分配一个链接,任何人都可以通过这个链接访问他的个人资料。我这样做是为了达到这个目的,但我怎么做都不行 我的URL.py from django.conf.urls import url from django.urls import path from django.contrib.auth import views as auth_views from . import views from .views import SearchResultsView # Template

我想为每个用户分配一个链接,任何人都可以通过这个链接访问他的个人资料。我这样做是为了达到这个目的,但我怎么做都不行

我的URL.py

from django.conf.urls import url
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views

from .views import SearchResultsView

# Template Urls!
app_name = 'accounts'

urlpatterns = [
    path('Skolar/',views.base, name= 'base'),
    path('Register/',views.register,name='register'),
    path('login/', views.my_login_view, name='login'),
    path('logout/',views.user_logout,name='logout'),
    path('<slug:username>/',views.profile, name='profile'),
    path('EditProfile/',views.update_profile,name='editprofile'),
    path('search/', SearchResultsView.as_view(), name='search'),
]

如何分配该链接,以便打开分配给该链接的用户配置文件。如果您知道如何在url中进行分配,请提供帮助。py:

path('my_profile/<int:pk>/', views.profile,name = 'my_profile'),
@login_required
def profile(request,pk):
    profile_data = Profile.objects.get(pk = pk)
    return render(request,'myprofile.html',{"profile_data":profile_data})

def allProfile(request):
    all_profile = Profile.objects.all()
    return render(request,'all_profile.html',{'all_profile':all_profile})
{% for pf in all_profile %}
......
    <a class="btn btn-sm" href="{% url 'my_profile' pf.id %}">{{pf.user_name}}</a>
......
{% endfor %}
all\u profile.html:

path('my_profile/<int:pk>/', views.profile,name = 'my_profile'),
@login_required
def profile(request,pk):
    profile_data = Profile.objects.get(pk = pk)
    return render(request,'myprofile.html',{"profile_data":profile_data})

def allProfile(request):
    all_profile = Profile.objects.all()
    return render(request,'all_profile.html',{'all_profile':all_profile})
{% for pf in all_profile %}
......
    <a class="btn btn-sm" href="{% url 'my_profile' pf.id %}">{{pf.user_name}}</a>
......
{% endfor %}
{%for pf在所有_profile%}
......
......
{%endfor%}
这是链接:

当用户点击这个链接时,他会进入他们的个人资料

<a class="btn btn-sm" href="{% url 'my_profile' request.user.profile.id %}">My Profile </a>

由于您使用外键将配置文件模型链接到内置用户模型,因此您只需构建一个HTML页面并将当前用户插入模板标记中即可

用于访问任何其他用户配置文件,

传递要作为url参数查看的配置文件的用户名。在views.py中接收参数

视图.py

> def profile (request, username): 
>     user = User.objects.get(username=username)
def userlist(request):
    list_user= Profile.objects.all()
    return render(request,'all_profiles.html',{'list_user':list_user})
path('profile/<int:pk>/', views.profile, name = 'profile'), 

现在通过上下文词典发送用户,并在模板中呈现所需的详细信息。

假设显示了一组用户名,将用户详细信息(此处为pk)传递到URL参数中。请看这个例子

视图.py

> def profile (request, username): 
>     user = User.objects.get(username=username)
def userlist(request):
    list_user= Profile.objects.all()
    return render(request,'all_profiles.html',{'list_user':list_user})
path('profile/<int:pk>/', views.profile, name = 'profile'), 
这将使用上下文字典变量“list\u user”将所有用户的列表发送到模板

现在将接收到的用户列表注入HTML页面:

all_profiles.html

{% for uprofile in list_user %}
    <a href="{% url 'profile' uprofile.id %}">{{uprofile.username}}</a>
{% endfor %}
<h1>profile {{user_prof.username}} </h1>
    
<p>{{user_prof.email}}</p>
现在在profile.html中,您可以插入用户模型的任何属性

profile.html

{% for uprofile in list_user %}
    <a href="{% url 'profile' uprofile.id %}">{{uprofile.username}}</a>
{% endfor %}
<h1>profile {{user_prof.username}} </h1>
    
<p>{{user_prof.email}}</p>
profile{{user\u prof.username}
{{用户\教授电子邮件}


显示错误名称/shreyash错误/name“User”未定义请您解释一下您的代码好吗?shreyash我的代码由@SANGEETH SUBRAMONIAM完全解释。请阅读下面对我的代码的解释。