Skip to content

kvu-luong/python_auto_tool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Basic selenium setup

  • download python: Python
  • setup enviroment variable -> link to folder contain python.exe. reference
  • install selenium for python:
python -m pip install selenium

To open Chrome

  1. install package chromedriver-binary
python -m pip install chromedriver-binary
  1. import package
import chromedriver_binary

Build interface with Django

reference

Setup project

  1. Install virtual enviroment working with python
python -m venv venv
  1. Activate env on window
source venv/Scripts/activate 
  1. Install Django
(venv) $ pip install Django
  1. Create project
django-admin startproject nameOfProject
  1. Clear up project
mv nameOfProject/manage.py ./
mv nameOfProject/nameOfProject/* nameOfProject
rm -r nameOfProject/nameOfProject
  1. Run server
python manage.py runserver

Create app in project with Django

  1. Create app
python manage.py startapp nameOfApp
  1. Let project know app exist Path: nameOfProject/settings.py Tab: INSTALLED_APPS

Create view

Path: nameOfApp/views.py
Then create function name: nameFunctionOne.
That function will call file nameOfFileHtml.html

def nameFunctionOne(request):
	return render(request, 'nameOfFileHtml.html', {})
  1. Create html file in templates folder of app
mkdir nameOfApp/templates/
touch nameOfApp/templates/nameOfFileHtml.html
  1. 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
]
  1. 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

Setup Model connect to database

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

Add bootstrap, css or script

  1. 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 %}
  1. 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

Scrapy

Crawl data for auto tool

Setup virtual enviroment

  1. Install venv
  2. Active enviroment venv
 source venv/Scripts/active
  1. instal scrapy
pip install scrapy

Create spider for crawl data

Reference

  1. Create project
scrapy startproject nameOfProject
  1. Run crawl
scrapy crawl nameOfSpider

Ajax with Django

Reference WorkFlow

  1. Create model contains all field that we need in nameOfApp/models.py
  2. Create form at backend nameOfApp/forms.py
  3. 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
  1. At front-end, we render all field of form and write ajax function in nameOfApp/templates/index.html
  2. 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"),

License

KVU

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published