1

I'm trying to build a tic tac toe game. My Django site admin page is appearing blank and I am not able to solve it.

enter image description here

from django.db import models
from django.db.models import Q
from django.contrib.auth.models import User

GAME_STATUS_CHOICES ={
    ('F','First Player to move'),
    ('S', 'Second Player to move'),
    ('W', 'First Player Wins'),
    ('L', 'Second player Wins'),
    ('D', 'Draw')
}

class GameQuerySet(models.QuerySet):
    def games_for_user(self, user):
        return self.filter(
            Q(first_player = user) | Q(second_player = user) 
    )

def active(self):
    return  self.filter(
        Q(status='F') | Q(status='S')
    )


class Game(models.Model):
    first_player = models.ForeignKey(User, related_name = 
        "game_first_player", on_delete=models.CASCADE)
    second_player = models.ForeignKey(User, related_name = 
        "game_second_player", on_delete=models.CASCADE)
    start_time = models.DateTimeField(auto_now_add=True)
    last_active = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=1, default='F', 
    choices=GAME_STATUS_CHOICES)
    objects =  GameQuerySet.as_manager()
    def __str__(self):
        return "{0} vs {1}".format(
            self.first_player, self.second_player)

class Move(models.Model):
    x = models.IntegerField() # X and Y co-ordinates
    y = models.IntegerField()
    comment = models.CharField(max_length=300, blank= True)
    by_first_player = models.BooleanField()

admin.py

from django.contrib import admin
from .models import  Game, Move

admin.site.register(Move)
@admin.register(Game)
class GameAdmin(admin.ModelAdmin):
    list_display = ('id','first_player','second_player','status')
    list_editable = ('status',)

I'm a newbie to Django. I have been stuck on this problem for a while now. Couldn't find the answers.

My settings.py seems to fine,

INSTALLED_APPS = [
     'django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'gameplay',
     'player',
]
Sheshan
  • 51
  • 3
  • 6

1 Answers1

0

On your settings file make sure your admin app is listed first

DJANGO_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'rest_framework.authtoken',
    'django_filters',

)

in admin - register to admin after admin class code (i know the admin decorator should work ....)

from django.contrib import admin
from .models import  Game, Move

class GameAdmin(admin.ModelAdmin):
    list_display = ('id','first_player','second_player','status')
    list_editable = ('status',)

class MoveAdmin(admin.ModelAdmin):
    list_display = MoveAdmin._meta.get_all_field_names()


admin.site.register(Game, GameAdmin)
admin.site.register(Move, Moveadmin)
Ohad the Lad
  • 1,889
  • 1
  • 15
  • 24
  • my admin app is listed in the `INSTALLED_APPS`. I dont know why its showing blank for me – Sheshan Dec 13 '18 at 03:13
  • tried your update but no results the admin site is empty as before – Sheshan Dec 14 '18 at 02:59
  • Did you ever run 'python manage.py migrate'? I mean initial migrate for the project. Also, add site to installed apps – Ohad the Lad Dec 14 '18 at 10:33
  • I would check if you are running the runserver command from the virtual env where the code lives, or maybe from out side the docker or virtual machine. It should work, it is something easy that u do not see. Try a new project from scratch and activate the admin. – Ohad the Lad Dec 16 '18 at 07:31