0

i have two model which are Post and Profile. i am keepin blog datas which are title,body,owner,slug etc in Post. and keepin' user profile settings which are slogan,email,website etc. in Profile

in my index.html page i display user profile infos and post lists in same page. so ; i need to connect these two models each other. when someone goes to 127.0.0.1/blog/username (with or without login) all the data which are belong to user named 'username' must be there.

here is my models.py:

class Profile(models.Model):
 slogan = models.TextField(blank=True)
 twitter = models.CharField(max_length = 100,blank=True)
 web_site = models.CharField(max_length=100,blank=True)
 email = models.CharField(max_length = 100,blank=True)
 def __unicode__(self):
 return self.slogan
class Post(models.Model):
 owner = models.ForeignKey(User)
 title = models.CharField(max_length = 100)
 body = models.TextField()
 bodyPreview = models.TextField() #preview için body alanı
 titlePreview = models.CharField(max_length=100) # preview için title alanı
 slug = AutoSlugField(populate_from='title' ,unique=True)
 posted = models.DateField(auto_now_add=True)
 isdraft = models.BooleanField(default=False)
 def __unicode__(self):
 return self.title
 @permalink
 def get_absolute_url(self):
 return ('view_blog_post',None,{'postslug':self.slug})

and my index view :

def index(request,username):
 post_list = Post.objects.filter(owner__username=username).filter(isdraft=False).order_by("-posted")
 p_index = Paginator(post_list,3) #anasayfa için pagination.[her sayfada 3 post]
 page = request.GET.get('page')
 try:
 indexPag = p_index.page(page)
 except PageNotAnInteger:
 indexPag = p_index.page(1)
 except EmptyPage:
 indexPag = p_index.page(p_index.num_pages)
 ## i need to get user's profile datas here. ??
 return render_to_response('index.html',
 {'post_list':post_list,'p_index':indexPag,'profile':query},
 context_instance = RequestContext(request))
asked Sep 20, 2012 at 11:17
2
  • 1
    Where is the FK to User from Profile? Commented Sep 20, 2012 at 11:32
  • and after FK ; how should i reach Profile Model's fields from view? Commented Sep 20, 2012 at 11:41

3 Answers 3

1

I think you should change your Profile model and add a OneToOne relationship to the User model(for more info see here):

class Profile(models.Model):
 user = models.OneToOneField(User)
 ...
class Posts(models.Model):
 author = models.ForeignKey(User)
 ...

and then in your views you can do:

user = get_object_or_404(User, username=username)
post_list = Post.objects.filter(author=user).filter(isdraft=False).order_by("-posted") 
return render_to_response('index.html', 
 {'post_list': post_list, 'user': user, ...}, ...)

And then in your template you are able to access the user's profile.More here e.g {{user.get_profile.slogan}}

answered Sep 20, 2012 at 11:30
Sign up to request clarification or add additional context in comments.

Comments

0

In your view:

def index(request, username):
 user = User.objects.get(username=username)
 return render(request, 'index.html', {'user':user})

In your template:

{{ user.post_set }}

and You will receive list of posts of current user.

answered Sep 20, 2012 at 11:26

1 Comment

there is no trouble about gettin' post list. i need to get profile settings which belongs to user.
0

The most natural way to achieve that is to add a OneToOne relation between your Profile model and the django's User model.

from django.contrib.auth.models import User
class Profile(models.Model):
 user = models.OneToOneField(User)
 # More fields
class Article(models.Model):
 author = models.ForeignKey(User)
 # More fields

This way, you can access the profile data through a User object. Would be something like this:

user = User.objecets.get(username=username)
profile = user.profile

More info about this, you can read the django model fields documentation here and you can also see this cool answer about the diference between the ForeignKey with unique=True and the OneToOneField

I hope this helps

answered Dec 20, 2012 at 17:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.