Django/Python – I don't use my django admin page to Add information my site because the admin field is empty

Question:

I’m not sure what happened but for the first time ever, i’m unable to use my admin fields. The fields are empty, and i can’t use the models i can created on Django. I tried creating a new project but still no luck and i don’t know how to resolve this.

My Models.py is

from django.db import models

# Create your models here.
class PersonModel(models.Model):

    Gender = {
        ('Male','Male'),
        ('Female','Female'),
    }
    Adult = {
        ('Non-Adult','Non-Adult'),
        ('Adult','Adult'),
    }
    Date = models.DateTimeField(auto_now_add=True),
    Name = models.CharField(max_length=50, blank=True, null=True),
    Surname = models.CharField(max_length=50, blank=True, null=True),
    The_gender = models.CharField(max_length=10, blank=True, null=True),
    The_Adult = models.CharField(max_length=10, blank=True, null=True),

    def __str__(self):
        return self.Name

my admin.py is

from django.contrib import admin
from . models import PersonModel

# Register your models here.
admin.site.register(PersonModel)

views.py

from django.shortcuts import render
from . models import PersonModel

# Create your views here.
def Home(request):
    Persons = PersonModel.objects.all()
    return render(request, 'Practice_2_app/Home.html',
    {'Persons':Persons})

def The_form(request):
    return render(request, 'Practice_2_app/The_form.html',
    {})

This is how my admin fields looks like

Asked By: Sizwe

||

Answers:

maybe rename . models to .models? <-not a problem

can you try it like this?

from django.contrib import admin
@admin.register(PersonModel)
class PersonModel(admin.ModelAdmin):
     list_display = ['date', 'name', 'surname', 'the_gender', 'the_adult',]
Answered By: Vinko Oakniiv

Could you try to register default user model unless you have created your own custom admin.

from django.contrib.auth.models import User
from django.contrib import admin
from . models import PersonModel

# Register your models here.
admin.site.register(User, PersonModel)

If it doesn’t work, could you also try to unregister the default user model and then register again the default one along with the custom created model.

from django.contrib.auth.models import User
from django.contrib import admin
from . models import PersonModel

# Register your models here.
admin.site.register(User, PersonModel)
Answered By: Ipvikukiepki-KQS

Update: I copied your code to a local project of my own and figured out your problem:

You have commas at the end of all of your lines in your model definition, so you’ve accidentally defined a bunch of tuples which are not valid fields in your model. If you look in your migrations, you can see that your PersonModel only has an id field, which would explain why you see an empty form in the admin.

# Generated by Django 3.2 on 2022-12-26 19:52

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='PersonModel',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
            ],
        ),
    ]

Previous:

It looks like your app is setup correctly, but the forms are simply not being rendered. The models are clearly registered so I don’t think there’s anything wrong with your models or admin code. Here are some things to try:

Make sure you aren’t overriding any of the default admin templates with custom code or with code from other packages. I’d try a couple of things. Remove any installed Django packages that could be overriding built in admin templates. With no luck there, you can try visiting in a different browser, try visiting with add-ons disabled, try visiting in incognito mode to make sure it’s actually a problem with your dev setup. I’d also try re-installing Django in your development environment.

Answered By: DragonBobZ
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.