Reputation:
In my website when a User log in he's redirected to his profile page. Now I'd like to see all the items he stored in the database. How could I do? Thanks
Here's the views.py. This is the page the user is redirected to after the login
class userView(TemplateView):
template_name = 'search/user.html'
Html file:
<div class="add">
<div class="posted">
{% if objects_list %}
{% for o in objects_list %}
<div class="container_band">
<div class=album_band>
<!-- insert an image -->
<img src= "" width="100%">
</div>
<div class="info_band">
<!-- insert table info -->
<table>
<tr><th><h2>{{o.band}}</h2></th></tr>
<tr><td> Anno: </td><td> {{o.anno}} </td></tr>
<tr><td> Disco: </td><td> {{o.disco}} </td></tr>
<tr><td> Etichetta: </td><td> {{o.etichetta_d}} </td></tr>
<tr><td> Matrice: </td><td> {{o.matrice}} </td></tr>
</table>
</div>
</div>
{% endfor %}
{% endif %}
</div>
models.py
class Info(models.Model):
utente = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, default=1)
band = models.CharField(max_length=200)
disco = models.CharField(max_length=200)
etichetta_p = models.CharField(max_length=200)
etichetta_d = models.CharField(max_length=200)
matrice = models.CharField(max_length=200)
anno = models.PositiveIntegerField(default=0)
cover = models.ImageField(upload_to='images/', blank=True)
def __str__(self):
return self.band
class Meta:
verbose_name_plural = "Info"
ordering = ['anno']
Upvotes: 1
Views: 44
Reputation: 1994
Use a Django view that takes a model and yields the model data.
class userView(ListView):
model = Info # assumes it is imported from models
template_name = 'search/user.html'
This will list everything, but you want to limit to the user only. Therefore, filter the output by the user id:
class userView(ListView):
model = Info # assumes it is imported from models
template_name = 'search/user.html'
def get_queryset(self):
qs = super().get_queryset()
return qs.filter(utente=self.request.user)
Change in your html template objects_list
to object_list
.
For further reference, see here: https://docs.djangoproject.com/en/3.0/ref/class-based-views/generic-display/#listview
Upvotes: 1