Skip to content

Instantly share code, notes, and snippets.

@chankruze
Last active April 25, 2019 12:57
Show Gist options
  • Save chankruze/4e8bbf5cb1b47b26dc889512cfcebd50 to your computer and use it in GitHub Desktop.
Save chankruze/4e8bbf5cb1b47b26dc889512cfcebd50 to your computer and use it in GitHub Desktop.
  • Create project django-admin startproject <project_name>

  • Create application

cd <project_dircetory>
python manage.py startapp <app_name>
  • Create view

    • index (in index app)
    • help (in help app)
  • Create app urls.py

    • home view
    • help view
  • Link app url.py to project urls.py

  • Add tamplates/static dir

mkdir <project_root_dir>/tamplates
mkdir <project_root_dir>/static

edit settings.py in project

TEMPLATE_DIR = os.path.join(BASE_DIR,"templates")
STATIC_DIR = os.path.join(BASE_DIR,"static")

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    STATIC_DIR,
]
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>First Django App !</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>This is help.html</h1>
{{help_text}}
</body>
</html>
from django.urls import path, re_path
from help import views
urlpatterns = [
re_path(r'^$', views.help, name='help'),
]
from django.shortcuts import render
# Create your views here.
def help(response):
help_screen = {"help_text":"This Is Your Help Desk !"}
return render(response, 'help/help.html', context = help_screen)
<!DOCTYPE html>
{% load staticfiles %}
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>First Django App !</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="{% static 'css/main.css' %}">
<script src="main.js"></script>
</head>
<body>
<h1>This is index.html</h1>
<!-- <img src="{% static 'images/test.png' %}" alt="Oh Fuck !"> -->
</body>
</html>
from django.urls import path, re_path
from first_app import views
urlpatterns = [
re_path(r'^$', views.index, name='index'),
]
from django.shortcuts import render
# Create your views here.
def index(request):
my_dict = {'insert_me':"Now I am inserted from first_app/index.html" }
return render(request, 'first_app/index.html', context = my_dict)
from django.contrib import admin
from django.urls import path, re_path, include
urlpatterns = [
re_path(r'^$', include('first_app.urls')),
re_path(r'^first_app/', include('first_app.urls')),
path('first_app/help/', include('help.urls')),
path('help/', include('help.urls')),
path('admin/', admin.site.urls),
]
@chankruze
Copy link
Author

Migration

(django_venv) chankruze@geekofia:~/Documents/chankruze/web_dev/DjangoStuff/hello_django$ python manage.py migrate
System check identified some issues:

WARNINGS:
?: (urls.W001) Your URL pattern '^$' uses include with a route ending with a '$'. Remove the dollar from the route to avoid problems including URLs.
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK
(django_venv) chankruze@geekofia:~/Documents/chankruze/web_dev/DjangoStuff/hello_django$ python manage.py makemigrations index
System check identified some issues:

WARNINGS:
?: (urls.W001) Your URL pattern '^$' uses include with a route ending with a '$'. Remove the dollar from the route to avoid problems including URLs.
Migrations for 'index':
  index/migrations/0001_initial.py
    - Create model Topic
    - Create model Webpage
    - Create model AccessRecord
(django_venv) chankruze@geekofia:~/Documents/chankruze/web_dev/DjangoStuff/hello_django$ python manage.py migrate
System check identified some issues:

WARNINGS:
?: (urls.W001) Your URL pattern '^$' uses include with a route ending with a '$'. Remove the dollar from the route to avoid problems including URLs.
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, index, sessions
Running migrations:
  Applying index.0001_initial... OK
(django_venv) chankruze@geekofia:~/Documents/chankruze/web_dev/DjangoStuff/hello_django$ 

@chankruze
Copy link
Author

quick check for database

(django_venv) chankruze@geekofia:~/Documents/chankruze/web_dev/DjangoStuff/hello_django$ python manage.py shell
Python 3.7.3 (default, Apr  3 2019, 05:39:12) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> 
>>> 
>>> 
>>> 
>>> from index.models import Topic
>>> print(Topic.objects.all())
<QuerySet []>
>>> t = Topic(top_name="Social Media")
>>> t.save()
>>> print(Topic.objects.all())
<QuerySet [<Topic: Social Media>]>
>>> quit()

@chankruze
Copy link
Author

create superuser

(django_venv) chankruze@geekofia:~/Documents/chankruze/web_dev/DjangoStuff/hello_django$ python manage.py createsuperuser 
System check identified some issues:

WARNINGS:
?: (urls.W001) Your URL pattern '^$' uses include with a route ending with a '$'. Remove the dollar from the route to avoid problems including URLs.
Username (leave blank to use 'chankruze'): 
Email address: [email protected]
Password: 
Password (again): 
The password is too similar to the email address.
Bypass password validation and create user anyway? [y/N]: N
Password: 
Password (again): 
Superuser created successfully.
(django_venv) chankruze@geekofia:~/Documents/chankruze/web_dev/DjangoStuff/hello_django$ 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment