Creating first Django project: admin web page won't open

NOTE. SPECIFIC QUESTION AT THE END BELOW. FIRST I DESCRIBE ALL THE STEPS THAT I WANT THROUGH :-))

Im using Python 2.7 and Django 4.2.1 on Windows.

I am creating a project called "mysite" which is the project used at docs.djangoproject.com.

So far I have done the following as in the tutorial:

1) printed: django-admin.py startproject mysite

This created all the standard folders which:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

      

2) type: manage.py runningerver and when I type http://127.0.0.1:8000

in the address bar, I got a congratulatory message.

So far so good ..

3) Then I edited the settings.py file. First I edited the DATABASE DATE and typed in the following:

'django.db.backends.sqlite3'

      

4) Still in settings.py, I specified the name DATABASE to create the database file. I named this:

'mysitedb'

      

5) Then I synchronized the database (for creating tables) by typing:

manage.py syncdb

      

6) Then the tutorial asks you to create an application called polls. So I typed:

manage.py startapp polls

      

This created the following folder and files:

polls/
    __init__.py
    admin.py
    models.py
    tests.py
    views.py

      

7) Then the tutorial suggests editing the models.py file by entering the following:

from django.db import models
import datetime
from django.utils import timezone

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __unicode__(self):
        return self.question

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField()

    def __unicode__(self):
        return self.choice_text

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

      

8) Moving forward, I then had to edit the INSTALLED_APPS section in the settings.py file by entering the following:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
)

      

9) Then I typed in the following to create the tables and their fields (I think ..):

manage.py sql polls

      

then the following information is printed:

BEGIN;
CREATE TABLE "polls_poll" (
    "id" integer NOT NULL PRIMARY KEY,
    "question" varchar(200) NOT NULL,
    "pub_date" datetime NOT NULL
);
CREATE TABLE "polls_choice" (
    "id" integer NOT NULL PRIMARY KEY,
    "poll_id" integer NOT NULL REFERENCES "polls_poll" ("id"),
    "choice_text" varchar(200) NOT NULL,
    "votes" integer NOT NULL
);
COMMIT;

      

10) Then I had to enter this to create tables (sync):

manage.py syncdb

      

Still so good so far ..

10) Then they suggest that I start a python shell in command and play with the database API. At this point everything was working fine. Can add data to table fields, etc.

But here's where it doesn't work. From part 2 of the tutorial. I need to start the server again (I did this: manage.py runningerver and it worked fine ..). I opened my browser webpage and typed http://127.0.0.1:8000/admin/

and I get the same "Works! Congratulations on your first Django page ..."

No error message. So how can I get the admin page with user logins (username and password)?

Any help would be appreciated. Thank!

+3


source to share


1 answer


Usually, when the admin in Django is down, the two main culprits forget to add the following two lines to the urls.py file.

admin.autodiscover()

      

and

url(r'^admin/', include(admin.site.urls))

      



Now you can continue learning and when you register the models in your admin.py, don't forget

admin.site.register(MyModelGoesHere, MyModelAdminNameGoesHere)

      

Good luck! :)

+5


source







All Articles