dimanche 11 mars 2018

how to display information entered by a user into a django model in the User profile

i'm kinda new to django, i'm working on a project currently. its a website where people can look for houses to rent. users will be able to create accounts, search for houses to rent and create listings about the houses they want to rent out.

i created a model to save all the information about houses that users want to rent out. i need to filter this information and display each users listing on their profile. ive searched online but no solution yet.

Really need help. models.py


    from django.db import models
    from django.contrib.auth.models import User
    class Myhouses(models.Model):
        Available = 'A'
        Not_Available = 'NA'
        Availability = (
            (Available, 'Available'),
            (Not_Available, 'Not_Available'),
        )
        name_of_accomodation = models.CharField(max_length=200)
        type_of_room = models.CharField(max_length=200)
        house_rent = models.IntegerField()
        availability = models.CharField(max_length=2, choices=Availability, default=Available,)
        location = models.CharField(max_length=200)
        nearest_institution = models.CharField(max_length=200)
        description = models.TextField(blank=True)
        image = models.ImageField(upload_to='profile_image')
        author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True, related_name='author')

        def __str__(self):
            return self.name_of_accomodation

view.py


    class ListingByUser(LoginRequiredMixin, generic.ListView):
        def get( self):  
            model = Myhouses
            template_name ='houses/ListingByUser.html'
            paginate_by = 10
        def get_queryset(self):
            return Myhouses.objects.filter(author=self.request.user)

urls.py


    from django.conf.urls import url, include
    from . import views
    from django.contrib.auth.models import User


    urlpatterns = [
        url(r'^addlisting/$', views.addlisting, name='addlisting'),
        url(r'^mylisting/', views.ListingByUser.as_view(), name='ListingByUser')
    ]

template


    





Aucun commentaire:

Enregistrer un commentaire