2012-12-25 21 views
2

這是我models.py文件我auth應用程序:這sql創建auth_article表]檢索Django的登錄定製SQL條目

from django.db import models 
class Article(models.Model): 
    title=models.TextField() 
    content=models.TextField() 

不過,我無法在登錄從sql檢索auth_article領域。 我正在使用User模型進行登錄。

from django.shortcuts import render_to_response 
from django.contrib.auth import authenticate, login 
from django.contrib.auth.models import User 
from auth.models import Article 

def login_user(request): 
    state = "Please login below..." 
    username = password = '' 
    if request.POST: 
     username = request.POST['username'] 
     password = request.POST['password'] 

     user = authenticate(username=username, password=password) 
     if user is not None: 
      if user.is_active: 
       login(request, user) 
       state = "You're successfully logged in!" 
       user_info = dict(username=username, email=user.email, fullname=user.get_full_name(), id=user.id) 
       aa=Article.objects.all() 
       return render_to_response('home.html', aa) 
... 
... 

回答

4

看那文檔選擇render_to_response https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render-to-response

應該是這樣:

return render_to_response('home.html', {'articles': aa}, RequestContext(request)) 
+0

我不能從文檔理解。我嘗試了一些東西,但只是搞砸了。可以請告訴我如何將'auth_article'的'sql'條目傳遞給我的'home.html'? – xan

+1

在我的代碼中就像詞典'{'articles':aa}'一樣。然後,您可以在模板「{%文章中的文章%}中使用它......' – sneawo

+0

謝謝了。我浪費了將近2天的時間。 但我還有一個疑問。我試圖用'creator'獲得'Article'對象,它等於上面'user_info'中的'id' [來自'user_info']。 但是, 'aa = Article.objects.get(creator = id)'不起作用。 – xan