Setting Up an Environment Within a virtualenv¶
Here I use nftwall as the project name and directory name.
mkdir nftwall
cd nftwall
virtualenv -p python .
source bin/activate
pip install django
mkdir src
cd src
django-admin startproject nftwall .
You can now start the project for testing:
python manage.py runserver
In nftwall/settings.py, enter the DNS name through which you access the service in the browser:
ALLOWED_HOSTS = ['host1.mydomain.de']
Sync the settings with the project.
python manage.py migrate
Change the listening IP and the hostname through which the service may be accessed.
python manage.py runserver [::]:8000
Create a new superuser
python manage.py createsuperuser
Creating Your First App¶
In the project root
python manage.py startapp products
Edit the models.py file in the products subdirectory
class Product(models.Model):
title = models.TextField()
description = models.TextField()
price = models.TextField()
summary = models.TextField()
Fill the admin.py file in the same directory (relative import)
from .models import Product
admin.site.register(Product)
Add the app in settings.py
INSTALLED_APPS = [
[...]
'products',a
[...]
]
After every change to models.py you need to run makemigrations and migrate.
python manage.py makemigrations
python manage.py migrate
You can also create records by using the Django command line.
python3 manage.py shell
from products.models import Product
Product.objects.create(title="test2", description="whatever", price="1234", summary="sweet")
Now let's make some modifications and first delete some files:
- Files in the migrations directory
- Delete pycache directory
- Delete sqlite db
Now back to models.py