[英]Django - How to pull a logged in users data from a database table?
I am relatively new to Django, and web development in general. 我对Django和Web开发比较陌生。 Basically, I want to pull a logged in users data from a database table so he can view/edit it on a 'My profile' page.
基本上,我想从数据库表中提取已登录的用户数据,以便他可以在“我的个人资料”页面上查看/编辑它。
I'm guessing the best way to do this would be to call the logged in users 'id', then somehow use it to call the row in the database table I want? 我猜最好的方法是将登录用户称为“ id”,然后以某种方式使用它来调用我想要的数据库表中的行?
This is how far I have got, but I don't want the user's 'id' in the url, I want the users 'row id' in the database table: 这是我能走的路,但是我不希望用户的“ id”出现在URL中,我希望用户的“行ID”出现在数据库表中:
urls.py urls.py
url(r'^my-profile/(?P<id>[0-9]+)', my_profile, name="my_profile"),
views.py views.py
user = request.user
template 模板
<a href="{% url 'my_profile' id=user.id %}">My profile</a>
Any help would be much appreciated! 任何帮助将非常感激!
My current code: 我当前的代码:
models.py models.py
class BarSignUp(models.Model):
LOCATION_CHOICES=(
("Central London", "Central London"),
("North London", "North London"),
("East London", "East London"),
("South London", "South London"),
("West London", "West London"),
)
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
name = models.CharField(max_length=50)
bio = models.CharField(max_length=50)
area = models.CharField(max_length=50, choices=LOCATION_CHOICES, null=True)
booking_link = models.CharField(max_length=100, null=True)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
def __str__(self):
return self.email
def get_absolute_url(self):
return reverse("barber_profile", kwargs={"id": self.id})
As you already know, you can get the logged-in user with request.user
. 如您所知,您可以使用
request.user
获取登录用户。 Therefore you can remove the id from the regex and use r'^my-profile/$'
. 因此,您可以从正则表达式中删除ID并使用
r'^my-profile/$'
。
Then change your url tag to {% url 'my_profile' %}
. 然后将您的url标记更改为
{% url 'my_profile' %}
。
Your BarSignUp
model has a ForeignKey on User
, which mean you can have zero, one, or many (for the largest possible definition of "many" - could be literally millions) of BarSignUp
records related to one given user. 您的
BarSignUp
模型在User
上具有ForeignKey,这意味着您可以拥有与一个给定用户相关的BarSignUp
记录的零个,一个或多个(对于“ many”的最大可能定义-可能是数百万个)。 If that's really what you want, then request.user.barsignup_set.all()
will return them all (cf the FineManual ). 如果这确实是您想要的,则
request.user.barsignup_set.all()
将全部返回(参见FineManual )。
If what you want is one single BarSignUp
per user, then you want a OneToOne
field instead of a ForeignKey
, and once you've updated your models you can get the current logged-in user BarSignUp
with request.user.barsignup
( this is document too ) 如果您想要的是每个用户一个
BarSignUp
,那么您想要一个OneToOne
字段而不是ForeignKey
,并且在更新模型后,可以使用request.user.barsignup
获取当前登录的用户BarSignUp
( 这是文档也 )
Foreignkey is a Many-to-One relationship.So if you want to access all the logged-in users BarSignUp data, then you should do the following. Foreignkey是多对一关系。因此,如果要访问所有已登录用户的BarSignUp数据,则应执行以下操作。
views.py views.py
signups = request.user.barsignup_set.all()
template 模板
{% for barsignup in signups %}
{{ barsignup }}
{{ barsignup.field }}
{% endfor %}
Also there is an error in you BarSignUp str method, please correct the same. BarSignUp str方法中也有错误,请更正。
def __str__(self):
return self.user.email #if you want to display user email
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.