2013-03-18 104 views
1

enter image description here我正在用Windows Vista上的Python 2.7在Django 1.4.3中进行编程。我正在尝试为用户输入功能,例如localhost:8000/admin中的电话,然后显示在前面的网页上。我有index.html用于前页面:显示值Django Python

<!-- Shows the label on tab. --> 
{% block title %} Inicio - Bienvenidos {% endblock %} 
{% block content %} 
<p> Esta es mi primera pagina en Django </p> 
{% if user.is_authenticated %} 
    <p> Bienvenido {{user.username}} </p> 
    {% if user.get_profile.photo %} 
     <img src="/media/{{user.get_profile.photo}}" width="100px" height="100px"/> 
    {% endif %} 
    {% if user.get_profile.telefono %} 
     <p> Numero Tel:</p> {{user.get_profile.telefono}}</p> 
    {% endif %} 
{% endif %} 
{% endblock %} 

而且它拉动models.py定义的输入值:

from django.db import models 
from django.contrib.auth.models import User 
# Create your models here. 

class userProfile(models.Model): 

    def url(self, filename): 
     ruta = "MultimediaData/Users/%s/%s"%(self.user.username, filename) 
     return ruta 

    user = models.OneToOneField(User) 
    photo = models.ImageField(upload_to = url) 
    telefono = models.CharField(max_length = 30) 

    def __unicode__(self): 
     return self.user.username 

然而,经过I输入的telefono(电话)值,前页不显示它。我附上了我收到的头版。该数字应显示在鱼片图像下。什么似乎是问题?

+0

你能后的HTML输出,所以我们可以看到被呈现? – Nitzle 2013-03-18 01:21:14

+0

此外,由于您使用的是Django 1.5,您应该知道'get_profile'方法已被[弃用](https://docs.djangoproject.com/en/1.5/releases/1.5/#auth-profile-module) 。 – Nitzle 2013-03-18 01:24:15

+0

大家好。我没有编辑正确的HTML和PY文件。我只是迁移了我的工作目录,但Notepad ++仍然有这些文件的记录。问题解决了。电话现在显示。感谢所有帮助过的人。 – Dombey 2013-03-18 04:46:37

回答

1

它看起来像你有价值评论。 <!---->被视为HTML中的评论。

从技术上讲,django模板仍在渲染值,因为它们使用不同的评论格式,但浏览器会看到评论标记,然后隐藏内容。

改变这一行:

<p> Numero Tel:</p> <!--{{user.get_profile.telefono}}</p>--> 

要这样:

<p> Numero Tel:</p> {{user.get_profile.telefono}}</p> 
+1

@Dombey哦,好的,您可以从项目根目录运行'python manage.py shell',并加载用户的配置文件以向我们展示'user.get_profile.telefono'输出的内容吗? – Nitzle 2013-03-18 01:27:07

+0

@Dombey在你加载你的shell之后,你需要从'django.contrib.auth.models'导入User',并加载指定的用户对象。从你原来的文章看来,你的用户名是root,所以'user = User.objects.get(username ='root')'会获取你的用户。然后你可以运行'user.get_profile.telefono'。 – Nitzle 2013-03-18 01:32:25

+0

这是否得到了'AttributeError:'函数'对象没有属性'telefono'。 – Dombey 2013-03-18 01:36:47

1

我会确保你正在编辑的页面你认为你正在编辑。将第一行内容更改为:

<p> Esta es mi segunda pagina en Django. {{user.get_profile.telefono}}</p> 

并确保文本更改为segunda。

+1

你说得对。我没有编辑正确的HTML和PY文件。我只是迁移了我的工作目录,但Notepad ++仍然有这些文件的记录。问题解决了。谢谢大家。 – Dombey 2013-03-18 04:45:03

1

要获得用户的扩展信息:

View = request.user.get_profile().field_name 
Template = user.userProfile.field_name 

所以显示用户模板扩展信息,它必须是

{% block title %} Inicio - Bienvenidos {% endblock %} 
{% block content %} 
<p> Esta es mi primera pagina en Django </p> 
{% if user.is_authenticated %} 
    <p> Bienvenido {{user.username}} </p> 
    {% if user.userProfile.photo %} 
     <img src="/media/{{user.userProfile.photo}}" width="100px" height="100px"/> 
    {% endif %} 
    {% if user.userProfile.telefono %} 
     <p> Numero Tel:</p> {{user.userProfile.telefono}}</p> 
    {% endif %} 
{% endif %} 
{% endblock %}