Home > Blockchain >  Login with another user gives everything that is created by the superuser instead of giving an empty
Login with another user gives everything that is created by the superuser instead of giving an empty

Time:03-03

I have a login page and register page where a user can create an account and login to his account, the problem is that: when i login with different user it's gives me everything that is created by the superuser or other user's instead of showing an empty page that could let me create fresh Album and Primary for the new user, Just like Facebook when you create an account and login it's will shows you an empty page in your Account. No friends no new Post and so on. how can i do this???

the login views:

def login(request):
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']

        user = auth.authenticate(username=username, password=password)

        if user is not None:
            auth.login(request, user)
            return redirect('home')
        else:
            messages.info(request, 'Invalid Credential') 
            return redirect('login')
    else:        
        return render(request, 'login.html')  

the register view:

def register(request):
    if request.method == 'POST':
        username = request.POST['username']
        email = request.POST['email']
        password = request.POST['password']
        password2 = request.POST['password2']

        if password == password2:
            if User.objects.filter(email=email).exists():
                messages.info(request, 'Email or user name Already taking')
                return redirect('register')
            elif User.objects.filter(username=username).exists():
                messages.info(request, 'username is taken')
                return redirect('register')
            else:
                user = User.objects.create_user(username=username, email=email, 
                password=password)
                user.save();
                return redirect('login')
        else:
            messages.info(request, 'Password Not Match')
            return redirect('register')   
        return redirect ('/')     
    else:
        return render(request, 'signup.html')

the models:

class Album(models.Model):
    image = models.ImageField(null=False, blank=False)
    name = models.CharField(max_length=100, null=False, blank=False)

    def __str__(self):
        return self.name       

class Primary(models.Model):
    profilePicture = models.ImageField(blank=False, null=False)
    firstName = models.CharField(max_length=25, blank=False, null=False) 
    sureName = models.CharField(max_length=25, blank=False, null=False) 
    lastName = models.CharField(max_length=25, blank=False, null=False)  
    address = models.CharField(max_length=50, blank=False, null=False)
    classOf = models.CharField(max_length=20, blank=False, null=False)
    yearOfGraduations = models.CharField(max_length=20, blank=False, null=False)
    hobbies = models.TextField(blank=False, null=False)
    dateOfBirth = models.CharField(max_length=20)
    year = models.ForeignKey(Album, on_delete=models.CASCADE)

    def __str__(self):
        return self.firstName
 

the Album Templates:

    <div >
    <div >
        <div >
            <a href="{% url 'post_create' %}">Create Album</a>
        </div>

        <!--Albums-->

        {% for my_album in albums %}
        <div >
            <div  style="width: 18rem;">
                <img src="{{ my_album.image.url }}" alt="" >
                <div >
                    <p>{{ my_album.name }}</p>
                    <br>
                    <a href="{% url 'view-Album' my_album.pk %}">viewAlbum</a>
                </div>
            </div>
        </div>
        {% endfor %}
    </div>
</div>

the Album view:

def primary(request):
    albums = Album.objects.all()
    return render(request, 'primary.html', {'albums': albums})

the viewAlbum templates:

    <div >
    <div >
        <div >
            <a href="{% url 'students' %}" >Create Students</a>
        </div>
    </div>
</div>
<br>
<div >
    <div >
        {% for prima in primaries %}
        <div >
            <div  style="width: 18rem;">
                <img src="{{ prima.profilePicture.url }}" alt="" >

                <div >
                    <p>{{ prima.firstName }}</p>
                    <br>
                    <a href="{% url 'view-Student' prima.pk %}">view Students</a>
                </div>
            </div>
        </div>
    </div>
    {% endfor %}
</div>

the viewAlbum view:

def viewAlbum(request, pk):
    post = get_object_or_404(Album, id=pk)

    my_album = Album.objects.get(id=pk)
    primaries = Primary.objects.filter(year_id=my_album.pk)
    return render(request, 'viewAlbum.html', {'primaries': primaries, 'post':post, 
    'my_album':my_album})

CodePudding user response:

you could add a ForeignKey to the Album:

class Album(models.Model):
    image = models.ImageField(null=False, blank=False)
    name = models.CharField(max_length=100, null=False, blank=False)
    user = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE)

then save each album with the user creating it.

And then filter the album in the view:

def primary(request):
    albums = Album.objects.filter(user=request.user)
    return render(request, 'primary.html', {'albums': albums})
  • Related