我已经使用Django创建了一个项目。登录和注册工作正常,但是我无法在个人资料页面上显示模型User
数据,我已经尝试了很多方法,但是没有显示信息。仅显示与管理员用户相关的数据。
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': context_dict})
urls.py
url(r'(?P<maas_username_slug>\w+)/$', views.maas, name='profile'),
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="")
profile.html
{%extends 'testapp/base.html'%}
{%block body_block%}
<h1>Profile page</h1>
<h1>{{ maas_username }} welcome to your profile page</h1>
<li>{{maas_username}}</li>
<li>{{maas_phone}}</li>
<li>{{maas_lastname}}</li>
{%endfor%}
答案 0 :(得分:0)
将context_dict
重命名为context
,并返回render(request, 'testapp/profile.html', context)
还有一件事,如果您的try block fail
比context will be None
为print nothing
更新
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 as ex:
# if your code get exception than context_dict will be none.
# that's why in html their is nothing to print.
# I said it above
context_dict['error'] = ex
print(context_dict)
return render(request, 'testapp/profile.html', context=context_dict)
在html
<h1>Profile page</h1>
<h1>{{ error }} </h1> <!-- here if error occured than error message will pring -->
<h1>{{ maas_username }} welcome to your profile page</h1>
<li>{{maas_username}}</li>
<li>{{maas_phone}}</li>
<li>{{maas_lastname}}</li>
答案 1 :(得分:0)
这是因为模板上的上下文与您的期望不同。在这种情况下,您可以在模板上访问以下值:
{%extends 'testapp/base.html'%}
{%block body_block%}
<h1>Profile page</h1>
<h1>{{ context_dict.maas_username }} welcome to your profile page</h1>
<li>{{ context_dict.maas_username}}</li>
<li>{{ context_dict.maas_phone}}</li>
<li>{{ context_dict.maas_lastname}}</li>
{%endfor%}
我建议您将渲染功能的context_dict
设置为context
参数,而不是为此创建新字典,或者只需要将context_dict作为第二个参数传递给渲染功能的参数。像这样:
return render(request, 'testapp/profile.html', context=context_dict)
或:
return render(request, 'testapp/profile.html', context_dict)
希望有帮助!
答案 2 :(得分:0)
代替
<h1>{{ maas_username }} welcome to your profile page</h1>
<li>{{maas_username}}</li>
<li>{{maas_phone}}</li>
<li>{{maas_lastname}}</li>
您需要将其更改为:
<h1>{{ context_dict.maas_username }} welcome to your profile page</h1>
<li>{{context_dict.maas_username}}</li>
<li>{{context_dict.maas_phone}}</li>
<li>{{context_dict.maas_lastname}}</li>
您要传递字典(单个对象)的原因,因此您需要尝试检索其键的值
答案 3 :(得分:0)
如果您将您的字典名称命名为“上下文” 那么可能是
{% for context in context %}
<h1>{{ context.maas_username }} welcome to your profile page</h1>
<li>{{ context.maas_username}}</li>
<li>{{ context.maas_phone}}</li>
<li>{{ context.maas_lastname}}</li>
{% endfor %}