Home > Net >  customer registration not showing in django admin
customer registration not showing in django admin

Time:12-27

Trying to create customer registration form for ecommerce website . No errors while submitting the form, but details are not shown in django admin . Here are required codes :

models.py

class Customer(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    full_name = models.CharField(max_length=200)
    address = models.CharField(max_length=200, null=True, blank=True)
    joined_on = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.full_name

urls.py

urlpatterns = [path("register",views.register, name="register")]

forms.py

class CustomerRegistrationForm(forms.ModelForm):
    username = forms.CharField(widget=forms.TextInput())
    password = forms.CharField(widget=forms.PasswordInput())
    email = forms.CharField(widget=forms.EmailInput())

    class Meta:
        model = Customer
        fields = ["username", "password", "email", "full_name", "address"]

    def clean_username(self):
        uname = self.cleaned_data.get("username")
        if User.objects.filter(username=uname).exists():
            raise forms.ValidationError(
                "Customer with this username already exists.")

        return uname

views.py

def register(request):
    form = CustomerRegistrationForm(request.POST or None)
    if form.is_valid():
     username = form.cleaned_data.get("username")
     password = form.cleaned_data.get("password")
     email = form.cleaned_data.get("email")
     user = User.objects.create_user(username, email, password)
     form.instance.user = user
     return redirect("/")

    return render(request,"register.html",{'form':form})

register.html

    {% load crispy_forms_tags %} 
 <div >
    <div >
        <div >
            <h3>Customer Registration Form</h3><hr>
            <form action="" method="POST">
                {% csrf_token %}
                {{form|crispy}}
                <button  >Register as a Customer</button>
            </form>
            <p> Already have an account? <a href="/home">Login here</a></p>
        </div>
    </div>
 </div>

Thank you in advance:)

CodePudding user response:

In your view, you're creating Users, not Customers.

If you're starting a new project, I would suggest using a custom user model.

If that's not possible, you need to save the form first to generate a Customer instance and then connect it with the appropriate User model:

def register(request):
    form = CustomerRegistrationForm(request.POST or None)
    if form.is_valid():
        username = form.cleaned_data.get("username")
        password = form.cleaned_data.get("password")
        email = form.cleaned_data.get("email")
        customer = form.save(commit=False)
        user = User.objects.create_user(username, email, password)
        customer.user = user
        customer.save()
        form.instance.user = user
        return redirect("/")

    return render(request,"register.html",{'form':form})
  • Related