NOTE: THE SPECIFIC QUESTION IS AT THE END BELOW. FIRST I DESCRIBE ALL THE STEPS I WENT 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 just like in the tutorial:
1)typed: django-admin.py startproject mysite
This created all the standard folders, which are:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
2) typed: manage.py runserver
and when typing http://127.0.0.1:8000 in the address bar I got a congratulations message.
So far, so good..
3) Then I edited the settings.py file. First I edited the DATABASE ENGINE and typed the following:
'django.db.backends.sqlite3'
4) Still in settings.py, I indicated a DATABASE name so that it creates a database file. I called it:
'mysitedb'
5) Then I synced the database (to create the tables) by typing:
manage.py syncdb
6) Then the tutorial asks to create an app called polls. So I typed:
manage.py startapp polls
That created the following folder and files:
polls/
__init__.py
admin.py
models.py
tests.py
views.py
7) The tutorial then asks to edit the models.py by typing 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 along, I then I had to edit the INSTALLED_APPS section in the settings.py file by typing 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 the following to create tables and their fields (I guess..):
manage.py sql polls
then the following printed out:
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 type this to create the tables (sync):
manage.py syncdb
So far, still so good..
10) Then they suggest I run python shell in command and play with the database API. At that point everything was working fine. Could add data in the tables’ fields, etc.
But here is where it doesn’t work. From PART 2 of the tutorial. I have to runserver again (I did that: manage.py runserver, and it worked fine..).
I opened a browser webpage and typed http://127.0.0.1:8000/admin/ and I get the same “It worked! Congratulations on your first Django-powered page…”
No error message. So how do I get the admin page with user logins (username and password) to appear?
Any help will be appreciated.
Thanks!
Usually when the admin in Django isn’t working the two main culprits are forgetting to add the following two lines to the urls.py file.
and
Now you can go ahead with the tutorial and when you register models in your admin.py, don’t forget
Good luck! 🙂