Home > Blockchain >  {% for projects in profile.project_set.all %} is not displaying anything
{% for projects in profile.project_set.all %} is not displaying anything

Time:06-26

I have been learning django through a video course and in the video course the guys has used

   {% for projectss in profile.project_set.all %}
       {{ projectss.title }}
   {% endfor %}

To display List of projects

Here is my Model.py file of project model

class Project(models.Model):
    owner = models.ForeignKey(Profile, null=True, blank=True, on_delete=models.SET_NULL)
    title = models.CharField(max_length=200)
    id = models.UUIDField(default = uuid.uuid4 , primary_key=True, unique=True, 
    editable=False)

And Here is my Model.py of Users model

class Profile(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE)
    id = models.UUIDField(default=uuid.uuid4, primary_key=True, editable=False, unique=True)

View.py file

def userProfile(request, pk):
    obj = Profile.objects.get(id=pk)
    context = {'user': obj}
    return render(request, 'users/user_profile.html', context)

The guy in video is doing the same thing its working for him but not me.

CodePudding user response:

Look at the context you are passing to the view:

context  = {'user': obj}

Change 'user' to 'profile' and you should be good.

def userProfile(request, pk):
    obj = Profile.objects.get(id=pk)
    context = {'profile': obj}
    return render(request, 'users/user_profile.html', context)
  • Related