我是django的新手;我无法在模型的概要文件模板上显示当前的登录用户详细信息。当我尝试使用Maas.objects.all()
时,我还将获得所有现有用户的数据。我只需要在模板上显示当前登录用户数据。当我尝试使用Maas.objects.get(username=username)
时,出现错误
Type error: Maas.testapp.models doesn't exist match query
我的 views.py :
def maas(request,maas_username_slug):
context_dict = {}
try:
maas = Maas.objects.get(slug=maas_username_slug)
context_dict['maas_username'] = maas.username
context_dict['maas_username_slug'] = maas_username_slug
context_dict['maas_phone'] = maas.phone
context_dict['maas_firstname'] = maas.firstname
context_dict['maas_lastname'] = maas.lastname
context_dict['maas_location'] = maas.location
context_dict['date_of_birth'] = maas.date_of_birth
context_dict['comments'] = maas.comments
context_dict['maas_gender'] = maas.gender
context_dict['photo'] = maas.photo
context_dict['maas'] = maas
except Maas.DoesNotExist:
pass
print(context_dict)
return render(request, 'testapp/profile.html', context_dict)
我的 profile.html :
<!DOCTYPE html>
{%extends 'testapp/base.html'%}
{%block body_block%}
<h1>Profile page</h1>
<h1>{{ maas_username }}</h1>
<li>Phone: {{ maas_phone }} </li>
<li>firstname: {{ maas_firstname }} </li>
<li>lastname: {{ maas_lastname }} </li>
<li> gender: {{ maas_gender }} </li>
<li> Comments: {{ comments }} </li>
{%endblock%}
我的 models.py :
from django.db import models
from django.template.defaultfilters import slugify
from django.contrib.auth.models import User
class Maas(models.Model):
username=models.CharField(max_length=25)
password = models.CharField(max_length=50)
phone = models.IntegerField(default=0)
firstname = models.CharField(max_length=40, blank=True)
lastname = models.CharField(max_length=40, blank=True)
location = models.CharField(max_length=40, blank=True)
email = models.EmailField(blank=True)
date_of_birth = models.DateField(null=True, blank=True)
gender = models.CharField(max_length=10, blank=True)
comments = models.CharField(max_length=500, blank=True)
photo = models.ImageField(upload_to='media/photos/', null=True,
blank=True)
slug = models.SlugField(unique=True,default="")
def __str__(self):
return self.username
def save(self, *args, **kwargs):
self.slug = slugify(self.username)
super(Maas, self).save(*args, **kwargs)
def __unicode__(self):
return self.username
class UserProfile(models.Model):
user = models.OneToOneField(User)
nickname = models.CharField(max_length=20, blank=True)
def __unicode__(self):
return self.user.username
我的 urls.py
urlpatterns = [
url(r'(?P<maas_username_slug>\w+)/$', views.maas,
name='profile'),
]
没有出现任何错误,但没有在 profile.html
上显示数据