Wednesday, October 30, 2013

django - differentiate users in the template

So, I am trying to create a user page, and its url will be associated with its username, something like this

http://domain.com/accounts/profile/robin/

And in that page, I want all the photo's uploaded by that user (here 'robin') to be displayed in the template. And another thing is, in the menu bar, there's an link for the the profile page for the request.user, i.e. the logged in user, suppose the user is 'kevin', and it will also direct it to the request.user's url like:

http://domain.com/accounts/profile/kevin/

Somehow I managed to get the desired output. But what I also wanted was that, even if I change the url to something different other than the request.user's name, I wanted the request.user and the other username to be differentiated easily, so that I can manipulate them in the templates. And so I hopefully came with this solution. But I really don't know that this is an appropriate solution. Because what if I change the username to something other than the request.user and come up with all its information. Please help me if there's any better way to do it. And feel free to ask me if you didn't understand the question. Any suggestion will be much appreciated! Thank you.

test.html:

  <body>            <div>              <ul>                  <li><a href="/photo/">Photos</a></li>                  <li><a href="/accounts/profile/{{user_1}}/">{{user_1.username}}</a></li>              </ul>             </div>            <div id="wrapper">              {% block content %}              {% endblock %}          </div>      </body>

profile_page.html:

  {% extends 'test.html' %}    {% block content %}  <p>{{user.username}}</p>    <div id="container">  {% for photo in photos %}  <img src="{{MEDIA_URL}}{{photo.image}}" width="300px" />  {% endfor %}  </div>    {% endblock %}

models.py:

   class Photo(models.Model):      title = models.CharField(max_length=200)      image = models.ImageField(upload_to=get_upload_file_name, blank=True)      pub_date = models.DateTimeField(default=datetime.now)      creator = models.ForeignKey(User, related_name="creator_set")      likes = models.ManyToManyField(User, through="Like")        class Meta:          ordering = ['-pub_date']          verbose_name_plural = ('Photos')         def __unicode__(self):          return self.title

app's urls.py:

      url(r'^profile/(?P<username>\w+)/$', 'userprofile.views.profile_page'),

views.py:

  @login_required  def profile_page(request, username):      if request.user.is_authenticated:          user_1 = request.user          user = User.objects.get(username=username)          user_photos = user.creator_set.all()          return render(request, 'profile_page.html', {'user':user,'photos':user_photos, 'user_1':user_1})

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2B4-nGownkbHyhDVrmq6iZ2fDxmc-jY-5uGCQR74%3D7MdcXak6A%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment