- download python: Python
- setup enviroment variable -> link to folder contain python.exe. reference
- install selenium for python:
python -m pip install selenium
- install package chromedriver-binary
python -m pip install chromedriver-binary
- import package
import chromedriver_binary
- Install virtual enviroment working with python
python -m venv venv
- Activate env on window
source venv/Scripts/activate
- Install Django
(venv) $ pip install Django
- Create project
django-admin startproject nameOfProject
- Clear up project
mv nameOfProject/manage.py ./
mv nameOfProject/nameOfProject/* nameOfProject
rm -r nameOfProject/nameOfProject
- Run server
python manage.py runserver
- Create app
python manage.py startapp nameOfApp
- Let project know app exist Path: nameOfProject/settings.py Tab: INSTALLED_APPS
Path: nameOfApp/views.py
Then create function name: nameFunctionOne.
That function will call file nameOfFileHtml.html
def nameFunctionOne(request):
return render(request, 'nameOfFileHtml.html', {})
- Create html file in templates folder of app
mkdir nameOfApp/templates/
touch nameOfApp/templates/nameOfFileHtml.html
- Register url to render html in project
Path: nameOfProject/urls.py
Then add name library
from django.urls import path, include
urlpatterns = [
path('', include(nameOfApp.urls)) #home page
]
- Register url to render html in app If this is the first time, we need to create file urls.py in app
touch nameOfApp/urls.py
In nameOfApp/urls.py. We register url for render file nameOfFileHtml.html
from nameOfApp import views #first time
urlpatterns = [
path('', views.nameFunctionOne, name='nameOfApp')
]
to call another file html we just need to define another function which call to another file html.
path('otherPath', views.nameOtherFunction, name='nameOfApp')
*** Remember: Declare templates url in nameOfProject/setting.py like nameOfProject/templates
Remember: manage.py have to place parallel with project folder makemigrations: create schema for database from model migrate: generate database
python manage.py makemigrations
python manage.py migrate
- Create file load html and js like header file Path: nameOfProject/templates
mkdir nameOfProject/templates/
touch nameOfProject/templates/header.html
in header.html we call css, js and block of other file like this
<link rel="stylesheet" href="" type="text/css">
{% block page_content %} {% endblock %}
<script src=""/>
to call file header.html in other file
{% extends "header.html" %}
{%block page_content %}
<div>Content of other file</div>
{% endblock %}
- Add static file like css, js, image
Create folder name 'static' then create file in this forder of App.
Path: nameOfApp/static
mkdir nameOfApp/static
touch nameOfApp/static/fileCssStatic.css
Then call it in template
{% load static %}
<link rel="stylesheet" type="text/css" href={% static 'fileCssStatic.css' %}
Usually, we call it in file header, so we just need to call it one time.
3. For CND link like bootstrap
We just copy link and paste it to header.html
Crawl data for auto tool
- Install venv
- Active enviroment venv
source venv/Scripts/active
- instal scrapy
pip install scrapy
- Create project
scrapy startproject nameOfProject
- Run crawl
scrapy crawl nameOfSpider
Reference WorkFlow
- Create model contains all field that we need in nameOfApp/models.py
- Create form at backend nameOfApp/forms.py
- Pass all data of Form and Model to view which we need in nameOfApp/views.py
from django.http import JsonResponse
from django.core import serializers
from .forms import nameOfForm
from .models import nameOfModel
- At front-end, we render all field of form and write ajax function in nameOfApp/templates/index.html
- Define url ajax function in nameOfProject/urls.py which we already created. Method 1:
path('post/ajax/specificName', nameOfFunction, name='urlNameCall'),
'post/ajax/specificName' can be any string we want. Just use for make distinct between other url.
In interface we view call function like below
$.ajax({
type: 'POST',
url: "{% url 'urlNameCall' %}",
data: dataSerializedData,
success: functin (rs){
},
error: function(rs){
}
})
Method 2: Define nameOfApp/views.py as View
from django.views import View
Then, in nameOfProject/urls.py, change path to
path('', nameOfClass.as_view(), name="urlNameCall"),