More Django

Today I've been learning about Django. This time I learned more about how to use templates within Django and stepping the admin page with a database. Most of the time was spent setting up the templates. The video did help explain how to use them but also the convention when using them.

At the beginning of the tutorial, I was adding most of the HTML on the individual pages with most of HTML is the same. The person in the video explains you should have a base HTML file where most HTML of the other pages can inherit from. This is so you have extra work to do if you want to change HTML where many pages share the same piece of code.

In the tutorial to make the pages more prettier we around a few pieces of code from the bootstrap website. The piece of code from the bootstrap website has CSS, HTML and some javascript. The chuck of code adds some colours and changes the font to look cleaner. Shortly in the tutorial we copy and paste snippets from the person GitHub account to add features like navigation bar and spacing between content and aside to make the website even more appealing. After adding most of the code snippets. I had to change the links for the navigation bar and other parts of the base HTML file.

After doing that, I moved to the next video which about the admin page. When starting a new Django project using the django-admin startproject mysite command. This will create a directory of all of the files and folders that are needed to develop the website. When running the server for the first time you can navigate to the admin page using /admin next to the web address. This won't work the first time as they are no accounts to log in into. But after you first set up a good amount of the public side of the website you could start to set up the admin system. As Django comes with an admin system with the HTML and CSS on the page. The developer mainly needs to focus on creating staff accounts and managing the database for this. This is great as making a separate admin page by starch will be tedious. So Django automates most of the tasks. Using this command python manage.py createsuperuser. Will make you set up a user for the admin page so you can log in. Django will prompt you for username, password and email address. If your making test site like I was you could use a make email address as is not important to log in as the page will only require the username and password. After making the account, you need to run the development server to so you can go to the admin page on your browser. Now you on the page you can log in using the information you used to make the user. Now you should see a page with the options with users and groups.

Second part

I stopped at setting up the admin page. Now carrying on from that. We need to create a database for the website. The database will later connect to the admin page again later. The database will already be set up from the startproject command which made all of the files and folders for the project. Django uses SQLite by default. As they are no extra installation needed for SQLite. As it comes with the python standard library. The next thing we need to do is define models. This is a very important bit if you want to deal with databases with Django.

A model is a way of defining your data in an object-oriented way. Defining models is done in the models.py file under your app's folder. To make a model you need to make a class with the name of your model. Then in the parenthesis add models. Model. Under that define the variables used in that model. For example, the Django documentation has an example of making a poll app. The model will be a question which will have a question_text variable which is defined with CharField with the max length of the character 200. And the publication data variable which is defined with a DateTimeField with a string next to saying data published.

This model should look like this:

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

Now with this, the model code can give Django lots of information to work with. Django can make a database schema from it using create table statements and other SQL commands. And a python database API to access the question and choice objects Now we need to connect the polls app to the Django project.

One of the great things about Django is that apps are plug and play meaning you can have apps can be used for multiple projects. Making lots of Django easy to reuse and creating more resources for future projects to do more repeatable work. Under the settings.py under the project folder, there should be a list in the code of the installed apps. It should look like this:

INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

You will need to add the path of your app. Because you're not copying a direct path like Django_tutorial\first_site\first_site\settings.py You will need to add a type of reference to the configuration class when adding it to the list. The Config class will be in the yourapp/apps.py file so to make the dotted path is yourapp.apps.pollsConfig. Now Django should know about the app you just coded up. Now you need to run this command:python manage.py makemigrations yourapp

After running this you should see something like this:

Migrations for 'polls':

polls/migrations/0001_initial.py:

- Create model Choice

- Create model Question

- Add field question to choice

By running this command makemigrations you simply telling Django you made changes to your models. Migrations are how Django saves changes of the models. This should create a file called 0001_initial.py under the migrations folder. They are not normally expected to readed often but they designed so you can edit them.

Tobi Olabode