I want to link to another user to show for example their public activities.
But I don't know how to link it via template.
This is my user profile model:
class UserProfile(models.Model):
user = models.OneToOneField(User)
name = models.CharField(max_length=64)
location = models.CharField(max_length=64, default='Tehran')
picture = models.ImageField(upload_to='profile_images', blank=True, default="/user_image/user_default.png")
slug = models.SlugField()
def save(self, *args, **kwargs):
self.slug = slugify(self.user)
super(UserProfile, self).save(*args, **kwargs)
# Override the __unicode__() method to return out something meaningful!
def __unicode__(self):
return self.user.username
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
But I cant use slug
because its not defined for the user profile before the users complete their profiles.
Please help me to find something like slug
for using in template to link to the other users like this:
<a href="{% url "index" user.profile.slug %}">{{ user.profile.name }}</a>
According to your code the slug field gets generated every time the user is saved. As it is though its not recommended to be used as a user identifier because 2 users with the same username will get the same slug.
It is recommended to use ids for now until you find a way to prevent duplicate slug names.
The:
{% url "index" user.profile.slug %}
can be used once you have a url that accepts the slug parameter like:
url(r'^(?P<slug>[\w-]+)/$', views.index, name='index')
where on the views.index view call you can do something like:
user = User.objects.get(slug=slug)
to get the user with the associated slug