본문 바로가기

Development (Python, Django, C..)

[Django] Tutorial 따라하기 - 1

* Reference : https://docs.djangoproject.com/ko/2.1/intro/tutorial01/#

 

#. Start Project

 

- Command check : django-admin

더보기

(djenv) ivan@django:~/djgo$ django-admin 

Type 'django-admin help ' for help on a specific subcommand. 

Available subcommands: 

[django] 
    check 
    compilemessages 
    createcachetable 
    dbshell 
    diffsettings 
    dumpdata 
    flush 
    inspectdb 
    loaddata 
    makemessages 
    makemigrations 
    migrate 
    runserver 
    sendtestemail 
    shell 
    showmigrations 
    sqlflush 
    sqlmigrate 
    sqlsequencereset 
    squashmigrations 
    startapp 
    startproject 
    test 
    testserver 
Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.). 

 

- Command check : django-admin startproject

더보기

(djenv) ivan@django:~/djgo$ django-admin startproject 
usage: django-admin startproject [-h] [--version] [-v {0,1,2,3}] 
                                 [--settings SETTINGS] 
                                 [--pythonpath PYTHONPATH] [--traceback] 
                                 [--no-color] [--template TEMPLATE] 
                                 [--extension EXTENSIONS] [--name FILES] 
                                 name [directory] 
django-admin startproject: error: You must provide a project name. 
(djenv) ivan@django:~/djgo$ django-admin startproject

 

- Execute startproject & checking result

(djenv) ivan@django:~/djgo$ django-admin startproject mysite . 
(djenv) ivan@django:~/djgo$ ls -al 
total 20 
drwxrwxr-x 4 ivan ivan 4096 Nov 18 12:31 . 
drwxr-xr-x 6 ivan ivan 4096 Nov 18 12:18 .. 
drwxrwxr-x 6 ivan ivan 4096 Nov 12 09:19 djenv 
-rwxrwxr-x 1 ivan ivan  538 Nov 18 12:31 manage.py 
drwxrwxr-x 2 ivan ivan 4096 Nov 18 12:31 mysite 
(djenv) ivan@django:~/djgo$ 

 

 



#. Run Server



- runserver on localhost

더보기

(djenv) ivan@django:~/djgo$ python manage.py runserver 
Performing system checks... 

System check identified no issues (0 silenced). 

You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. 
Run 'python manage.py migrate' to apply them. 

November 18, 2019 - 12:32:20 
Django version 2.0.13, using settings 'mysite.settings' 
Starting development server at http://127.0.0.1:8000/ 
Quit the server with CONTROL-C. 

 

- runserver for remote connection

(djenv) ivan@django:~/djgo$ python manage.py runserver 0:8000 
Performing system checks... 

System check identified no issues (0 silenced). 

You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. 
Run 'python manage.py migrate' to apply them. 

November 18, 2019 - 12:35:55 
Django version 2.0.13, using settings 'mysite.settings' 
Starting development server at http://0:8000/ 
Quit the server with CONTROL-C. 

 

- Check on browser (Chrom : https://192.168.56.200:8000 on other remote server)

(djenv) ivan@django:~/djgo$ python manage.py runserver 0:8000 
~~ 
Invalid HTTP_HOST header: '192.168.56.200:8000'. You may need to add '192.168.56.200' to ALLOWED_HOSTS. 
[18/Nov/2019 12:36:14] "GET / HTTP/1.1" 400 60018 
Invalid HTTP_HOST header: '192.168.56.200:8000'. You may need to add '192.168.56.200' to ALLOWED_HOSTS. 
[18/Nov/2019 12:36:14] "GET /favicon.ico HTTP/1.1" 400 60068 
^C(djenv) ivan@django:~/djgo$ 
 


- to Fix [Not allowed]

## to Fix 
(djenv) ivan@django:~/djgo$ more mysite/settings.py |grep ALLOW 
ALLOWED_HOSTS = ['192.168.56.200','.pythonanywhere.com'] 

 

 


#. Start APP

- Current directory status 

더보기

(djenv) ivan@django:~/djgo$ ls -al 
total 20 
drwxrwxr-x 4 ivan ivan 4096 Nov 18 12:32 . 
drwxr-xr-x 6 ivan ivan 4096 Nov 18 12:18 .. 
-rw-r--r-- 1 ivan ivan    0 Nov 18 12:32 db.sqlite3 
drwxrwxr-x 6 ivan ivan 4096 Nov 12 09:19 djenv 
-rwxrwxr-x 1 ivan ivan  538 Nov 18 12:31 manage.py 
drwxrwxr-x 3 ivan ivan 4096 Nov 18 12:32 mysite

 

- startapp

(djenv) ivan@django:~/djgo$ python manage.py startapp polls 
(djenv) ivan@django:~/djgo$ ls -al 
total 24 
drwxrwxr-x 5 ivan ivan 4096 Nov 18 12:41 . 
drwxr-xr-x 6 ivan ivan 4096 Nov 18 12:18 .. 
-rw-r--r-- 1 ivan ivan    0 Nov 18 12:32 db.sqlite3 
drwxrwxr-x 6 ivan ivan 4096 Nov 12 09:19 djenv 
-rwxrwxr-x 1 ivan ivan  538 Nov 18 12:31 manage.py 
drwxrwxr-x 3 ivan ivan 4096 Nov 18 12:32 mysite 
drwxrwxr-x 3 ivan ivan 4096 Nov 18 12:41 polls 

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

(djenv) ivan@django:~/djgo$ ls -al polls 
total 32 
drwxrwxr-x 3 ivan ivan 4096 Nov 18 12:41 . 
drwxrwxr-x 5 ivan ivan 4096 Nov 18 12:41 .. 
-rw-rw-r-- 1 ivan ivan   63 Nov 18 12:41 admin.py 
-rw-rw-r-- 1 ivan ivan   85 Nov 18 12:41 apps.py 
-rw-rw-r-- 1 ivan ivan    0 Nov 18 12:41 __init__.py 
drwxrwxr-x 2 ivan ivan 4096 Nov 18 12:41 migrations 
-rw-rw-r-- 1 ivan ivan   57 Nov 18 12:41 models.py 
-rw-rw-r-- 1 ivan ivan   60 Nov 18 12:41 tests.py 
-rw-rw-r-- 1 ivan ivan   63 Nov 18 12:41 views.py 
(djenv) ivan@django:~/djgo$ 

 



#. Create first view

- Change views.py file


(djenv) ivan@django:~/djgo$ more polls/views.py

from django.http import HttpResponse 


def index(request): 
    return HttpResponse("Hello, world. You're at the poll index" 

 

- Create URL fo view calling

(djenv) ivan@django:~/djgo$ more polls/urls.py

from django.urls import path 
from . import views 

urlpatterns = [ 
    path('', views.index, name='index'), 
] 


-  Include URL for polls

(djenv) ivan@django:~/djgo$ more mysite/urls.py

"""mysite URL Configuration 

The `urlpatterns` list routes URLs to views. For more information please see: 
    https://docs.djangoproject.com/en/2.0/topics/http/urls/ 
Examples: 
Function views 
    1. Add an import:  from my_app import views 
    2. Add a URL to urlpatterns:  path('', views.home, name='home') 
Class-based views 
    1. Add an import:  from other_app.views import Home 
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home') 
Including another URLconf 
    1. Import the include() function: from django.urls import include, path 
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls')) 
""" 
from django.contrib import admin 
from django.urls import include, path 

urlpatterns = [ 
    path('admin/', admin.site.urls), 
    path('pools/', include('polls.urls')), 
] 

 

- Start Server 

(djenv) ivan@django:~/djgo$ python manage.py runserver 0:8000 
Performing system checks... 

System check identified no issues (0 silenced). 

You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. 
Run 'python manage.py migrate' to apply them. 

November 18, 2019 - 13:09:17 
Django version 2.0.13, using settings 'mysite.settings' 
Starting development server at http://0:8000/ 
Quit the server with CONTROL-C. 


- Check on browser --> Error occurs 

더보기

Page not found (404) 
Request Method: GET 
Request URL: http://192.168.56.200:8000/ 
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order: 

pools/ 
admin/ 
The empty path didn't match any of these. 

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page. 


- to Fix bugs


(djenv) ivan@django:~/djgo$ more mysite/urls.py

~~ 
urlpatterns = [ 
    path('admin/', admin.site.urls), 
    path('polls/', include('polls.urls')),  <<<< syntax error 
] 

 

- Check browser : http://192.168.56.200:8000/polls   <---- Wrong address (/polls needed)

Hello, world. You're at the poll index





'Development (Python, Django, C..)' 카테고리의 다른 글

[Django] Tutorial 따라하기 - 4  (0) 2019.11.27
[Django] Tutorial 따라하기 - 3  (0) 2019.11.23
[Django] Tutorial 따라하기 - 2  (0) 2019.11.19
[Django] Starting Django!!  (1) 2019.11.16
[Python] File I/O  (0) 2019.04.27