--People who are curious about Django but can't do the tutorial because it seems difficult --People who gave up due to an error ――The explanation is good, so anyone who wants to finish it anyway
What you need
The following points are assumed so that it is easy to understand even from scratch. --Where to write the code is written as a file path ――Specify the parts of the code that you can change by yourself ――When you say where to change the file, you may not know where to change it, so basically when you rewrite the file, the contents of the rewritten file will be written as it is, so copy and paste from all selections.
django-admin startproject mysite
Put this in your terminal, django will create the required files and put them in place. You can change the "mysite" part to your liking and it's ok, but names like "test" will result in an error as the system will use them later. Make it somewhat unique.
python manage.py runserver
Copy this into your terminal as well, and you'll be able to see your project in your browser.
localhost:8000
You can access it by entering it as a URL in your browser.
The files you just created are called projects, and you will create apps one by one in this.
python manage.py startapp polls
This is also copied to the terminal. I will make a voting application called polls.
The view is what you see when the user accesses it.
view.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
Make the views file in the polls folder as above, copy and paste the above one as it is and delete the existing one. Then make polls / urls.py as follows.
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
Finally mysite / urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
If you edit it so that it becomes k.
The rest
python manage.py rumserver
In your terminal, look at localhost: 8000 / polls in your browser, and if you see Hello, world. You're at the polls index., You're done. Continue on to the next post.
You can also write Hello, world. You're at the polls index. When creating the 4th view in html grammar, for example.
view.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("<h1>My First Heading</h1><p>My first paragraph.</p>")
In this case, the text will be displayed as html.
Recommended Posts