2013-03-18 97 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 %}