Home > Software design >  How to show category names from a mysql database table in the dropdown list of django form
How to show category names from a mysql database table in the dropdown list of django form

Time:12-04

I am working on a article management platform webapp using django. I have created a registration form using the django form where I want to show category names from the category table.

This is the code to create category table where I have two column. One is cid which is ID and another one is category_name. Here the category name will be for example: Technology, Software engineering, Medicine etc.

blog.models.py

from django.db import models

# Create your models here.
class Category(models.Model):
    cid = models.AutoField(primary_key=True, blank=True)
    category_name = models.CharField(max_length=100)

    def __str__(self):
        return self.category_name

The cid is a foreign key for users table because each user must select a category name from the specialization field to register an account in this app. As I am using built-in user model, so I have added the cid as a foreign key in the user table as given below.

users/model.py

from django.db import models
from blog.models import Category
from django.contrib.auth.models import AbstractUser

# Create your models here.
class CustomUser(AbstractUser):
    cid = models.ForeignKey(Category, on_delete=models.CASCADE)

in the forms.py file I have added the email and specialization field to display them in the registration form like below. However, I am not sure if the category code part is okay or not. could you please look into it?
users/forms.py

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from blog.models import Category

class UserRegisterForm(UserCreationForm):
    email = forms.EmailField()

    category = Category()


    cid = forms.CharField(label='Specialization', widget=forms.Select(choices=category.category_name))


    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2', 'cid']

This is register.html file:

register.html file

{% extends "users/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
    <div >
        <form method="POST">
            {% csrf_token %}
            <fieldset >
                {{ form| crispy }}
            </fieldset>
            <div >
                <button  type="submit">Sign Up</button>
            </div>
        </form>
        <div >
            <small >
                Already Have An Account? <a  href="{% url 'login' %}">Sign In</a>
            </small>
        </div>
    </div>
{% endblock content %}

I want to show the category names here in the specialization dropdown list which will be come from the cateogry table but those category names are not showing in the dropdown list. Registration page UI

I am not understanding how to solve this problem. Could anyone help me out to solve this problem. What will be the coding part to solve this.

I tried to add category names in the specialization dropdown list but I failed.I want anyone to solve this problem

CodePudding user response:

First of all, category needs to be a field, not a class. Use ModelChoiceField for this.

  • Related