Home > Software design >  Heroku not recognising users
Heroku not recognising users

Time:06-04

Managed to install Heroku today and it looks like some functionality that work perfectly on my local disk are not recognised on Heroku.

On my local version: Users that register can leave a comment through a form. When accessing the form, the user that can select their name from a dropdown list and leave a comment against a specific product.

ON the Heroku version: the users that are registered on the app are not listed on the comment form. The list is empty.

I am happy to post some code, but not sure where to start and would end up copy/pasting a big chunk of code.

Have I done something wrong with the upload?

Again, the local verison work perfectly fine. So, it must be due to the transfer from Local to Heroku

Any pointers?

Edit - Views

from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth import logout
from django.contrib import messages
from .models import Product, User, Venue, ProductReview
from .forms import DrinkForm, VenueForm, ReviewAdd
from django.core.paginator import Paginator
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404


#from flask import request

# Create your views here.


...
@login_required
def add_review(request, product_id):
    product = get_object_or_404(Product, pk=product_id)
    form = ReviewAdd(request.POST or None) 
    if form.is_valid():
        new_rating = form.save(commit=False)
        new_rating.product = product
        new_rating.save()
        return redirect('home')
    return render(request, 'main/add_review.html',{'form':form})

Comment from SamSparx: Another thing you can check on your end is the users in your admin section for both environments - make sure you haven't forgotten any group memberships or permissions.

I think this is where the problem is. Registered users on the Heroku app are registered in the section "AUTHENTICATION AND AUTHORIZATION" but not in the "Main" section.

Looking at my local environment again, actually I must have changed something in the code, as all new users signed up do not get into the "Main" section either.

Do you know how I could fix that?

##Edit##

User Creation Code below.

Views.py

from django.shortcuts import render, redirect
from .forms import RegisterForm
from django.contrib.auth import login, authenticate, logout
from django.contrib import messages
from django.http import HttpResponseRedirect

# Create your views here.
def register(response):
    if response.method == "POST":
        form = RegisterForm(response.POST)
        if form.is_valid():
            new_user = form.save()
            new_user = authenticate(username=form.cleaned_data['username'],
                                    password=form.cleaned_data['password1'],)
            login(response, new_user)
            return HttpResponseRedirect("/home")
        #return redirect("/home")
    else:
        form = RegisterForm()
    
    return render(response,"register/register.html", {"form":form})

Models.py (user model, just in case)

from django.db import models
from django.contrib.auth.models import User
from decimal import Decimal
import locale
import logging


class User(models.Model):
    first_name = models.CharField('User Name', max_length=120)
    last_name = models.CharField('User LastName', max_length=120)
    email_address = models.EmailField('User Email Address')
    
    def __str__(self):
        return self.first_name  ' '  self.last_name

CodePudding user response:

I suspect the problem is your User class that you define in models.py file. There is already a User class in django.contrib.auth.models and you are already importing it in that file. When you create your own User in Models.py you are missing out on all the inbuilt django User stuff, because they are, in fact, separate classes. So I suspect trying to use those interchangeably is making things wonky.

There are a number of ways to handle Users.

  1. Just use the django User. If all you need is the fields and methods here, you don't need to define your own class. Just import it where you need it, and it's ready to go. From what I've seen above, this is the best option for you currently.

  2. If you think you may need extra fields in future, or may want to authenticate via the email field rather than the username, or basically change or extend the default User behaviour, you can either create a model with a one to one relationship field with the Django User, or (recommended in the docs) you can create a CustomUser that inherits from AbstractUser and define your extra fields on that model.

  3. Build one from scratch by inheriting from CustomBaseUser (way more complex)

NB:It's not recommended to inherit from or extend the default Django User. A CustomUser inheriting from AbstractUser is built to have everything Django User does and won't break anything else if you extend it.

  • Related