2

Here is my models.py file of my auth app: [which creates a auth_article table in sql]

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

However, I'm unable to retrieve auth_article fields from sql on login. I'm using the User models for login.

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)
...
...
xan
  • 4,640
  • 13
  • 50
  • 83

1 Answers1

4

Look at the render_to_response docs https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render-to-response

It should be like:

return render_to_response('home.html', {'articles': aa}, RequestContext(request))
sneawo
  • 3,543
  • 1
  • 26
  • 31
  • I can't understand from the docs. I tried doing some stuff, but only messing it up. Could be please tell me how can I pass `sql` entries of `auth_article` to my `home.html`? – xan Dec 25 '12 at 08:11
  • 1
    Like in my code as dictionary `{'articles': aa}`. Then you can use it in template `{% for article in articles %} ...` – sneawo Dec 25 '12 at 08:14
  • Thanks a ton. I wasted almost 2 days on it. But I had one more doubt. I was trying to get `Article` objects with `creator` equal to `id` [from `user_info`] above. But, `aa=Article.objects.get(creator=id)` didn't work. – xan Dec 25 '12 at 09:41
  • 1
    Should be `aa=Article.objects.filter(creator=user)` – sneawo Dec 25 '12 at 10:35
  • I managed to achieve it by doing: `aa=Article.objects.filter(creator=user.id)`. Still, can you explain it to me why can't i do `creator=id` when `id=user.id` was defined above? – xan Dec 25 '12 at 10:40
  • Also, I know I'm asking too much, could you answer this question? I'm a newbie in django/Python and struggling on my own out here. http://stackoverflow.com/questions/14029730/load-css-images-in-django – xan Dec 25 '12 at 10:45