Posts

Django channels — using WebSockets

Jan. 6, 2021

-two-way communication between web-server and client-page- Http protocol follow the strict procedural schema: client request- server … This is our starting project (based on Django 3.1.4): Before installing channels, be sure you have installed python3-devel package, … and running we will see that we already have a Django ASGI … Channels needs to use Redis (a Remote Dictionary Service, an … Redis could be available as a docker service — after … or as an individual service, with some methods of installation … 2) Python method redis-server install 3) or you could install from source. To verify that redis is active and listening on port … or test redis by running a python script (see docs): … At this point, we need to inform django project to … Let’s also ad an app as a playground space. Make ‘/config/urls.py’ as: And let to be ‘/app/urls.py’ add to ‘/app/views.py’ and app to ‘/config/settings.py’ create /app/templates/app/index.html To suppress warning messages, run also Now, we have a minimal Django project frame to dive … Corresponding WebSocket client, launched in frontend (html page) has the … /app/routing.py is the WebSocket equivalent of /app/urls.py For much fun is welcomed the addition of a clock … server async consumer which sends time data every 1 sec … But WebSockets would have limited usefullnes without the possibility of … Add new sockets in client page: and new entries in routing.py: Resource: git repository A more real application of grouping sockets by updating (syncronizing) … counter-channels git repository

Django — email user authenthication

Dec. 29, 2020

Initiate a python virtual environment and start a new project: Update /src/settings.py Update ‘/accounts/models.py’ Update ‘/accounts/forms.py’ Update ‘/src/admin.py’ Run: At this point we, in Django-custom user authentication , have … Update ‘src/settings.py’ Run Update ‘/src/urls.py’ Now we can access page ‘http://127.0.0.1:8000/accounts/login/’ which does not have … create ‘/templates/home.html’ (reverse links ‘account_logout’, etc are defined in django-allauth … Update ‘/src/urls.py’: Create ‘/accounts/urls.py’ And ‘/accounts/views.py’ Now, home page is accesible at ‘localhost:8000’.The project works with … return us the templates which needs adjusting the forms: and signup.html like that Resources:github projecttutorial

Django — custom user authentication

Dec. 29, 2020

Initiate a python virtual environment and start a new project: We do not migrate until the configuration of custom user … Update ‘/accounts/models.py’ Update ‘/accounts/forms.py’ Update ‘/src/admin.py’ since Admin is linked to the users definitions. Run: Create superuser: Update /src/settings.py adding: Create directory ‘custom_auth/templates/registration’ and edit ‘/templates/base.html’ (using Bootstrap 4 starter … Create ‘/templates/home.html’ ‘/templates/registration/login.html’ ‘/templates/registration/signup.html’ Update ‘/src/urls.py’ Create ‘/accounts/urls.py’ Update ‘/accounts/views.py’ Access ‘localhost:8000’.The end.Resources:github projecttutorial

Django — default (built-in) user authentication system

Dec. 29, 2020

Installing python packages: Creating project: instead of known command which generates the structure: we could use, alternatively: which generates structure: Django, automatically installed ‘django.contrib.auth’ modules in /src/settings.py which will perform … To use ‘auth’ we need to activate predefined urls of … After: and accesing django gives as a ‘Page not found (404)’ in which … Django ‘auth’ application has built-in url and views for login … Update /src/settings.py Create ‘/templates/base.html’ (using Bootstrap 4 starter template): Login and logoutWe need to create login and logout templates.First, … Create '/accounts/urls.py' Now, we are able to access home page at ‘http://localhost:8000/’ … After login we need a home redirect home page.‘/accounts/views.py’ Add to ‘/src/settings.py’ to redirect to home.html page after login: create ‘/templates/home.html’ As we don’t have any user, for simplicity, we could … or, we can create users in admin inteface.After logout we … Now, we have a login/logout authentication system working. We need … Add ‘crispy_forms’ to INSTALLED_APPS in ‘/src/settings’.Update’ /templates/registration/login.html’ Now, username and password fields from login.html form have equal … Update ‘/accounts/views.py’ with: create ’/templates/registration/signup.html’ Password resetFirst of all we need to activate the Django … The default reset templates are not very pretty so ve … ‘/templates/registration/password_reset_done.html’ ‘templates/registration/password_reset_complete.html’ Finnaly, update /templates/home.html When accesing ‘http://localhost:8000/accounts/password_reset/’, be sure that there are users that … Resources:github repositorytutorial

Understanding Django Class based views

Aug. 19, 2020

Class based views are "ready to apply" made views for … View Create Retrieve data:1) List 2) Detail Update Delete Some methods for customizing class based views:get_context_data() for defining additional … Examples are inspired by a geeksforgeeks article and are available …

Ghid Django Cpanel starter

Aug. 10, 2020

Ghid pentru instalarea aplicatiilor python/Django in Cpanel.1) logare in Cpanel2) … si fisierul passenger_wsgi.py care lanseaza aplicatia python.Default, acest fisier defineste … Pentru a lansa proictul Django mysite, trebuie sa modificam passenger_wsgi.py … 6)Open File Manager and rename file /home/username/public_html/index.html which is the …

Django Cpanel starter

Aug. 10, 2020

1)login on cpanel account2)Open Setup Puthon App3)Create application (say its … and a passenger_wsgi.py which is the wsgi starter of the … To launch our Django project, we must modify it to: 6)Open File Manager and rename file /home/username/public_html/index.html which is the …

Lxml howto

Aug. 9, 2020

Library used are: First, we are parsing the html string to an Element … Lxml library has search functions, but we could simply use … Element tree has attributes: tag(name of html element) , text(text … In our example, for appending a list after our div, … Creating and appending new elements is possible with etree.SubElement constructor … And, finnaly, retrive modified html source with: Download test file: lxml_test.py

XPath expressions

Aug. 9, 2020

SELECT BY PATH (parent/childrens... structures)Select element with absolute /html/body/aaa/bbb/cccSelect all … SELECT PARENT OF AN ELEMENTSelect parent of all elements type …

Python regex quick reference

Aug. 9, 2020

Search Patterns Example usage: Links:https://www.python-course.eu/python3_re.phphttps://www.python-course.eu/python3_re_advanced.phphttps://github.com/amalshehu/legendary-regex/blob/master/README.mdhttps://www.rexegg.com/regex-lookarounds.htmlhttps://www.regular-expressions.info/lookaround.html

GET vs. POST

Aug. 7, 2020

Basic distinctions from GET and POST request are below: For … As a test example we will use the forms below. … The same requests are reproduced below, in different contexts:

Json

Aug. 7, 2020

Basic JSON usage example in Django environmentThe general scheme by … In this page, we had sent to the server the … and received json is: The result is dispayed below. Client source is: and server source is:

Form sending data vs. Ajax data submission

Aug. 7, 2020

1) The default content type of a form is:enctype = … 2) Uploading files needs: enctype = "multipart/form-data" 1) Sending data without files with ajax needs setting 'Content-Type' …

XmlHttpRequest (III)

Aug. 7, 2020

If we try to reuse the same code for two … Simply separating the requests makes things work but don't resolve … So every request must be a separate object...

XmlHttpRequest (II)

Aug. 7, 2020

GET requestWe used here err_codes.js to describe html error codes. POST request

XmlHttpRequest (I)

July 22, 2020

GET requestExample below inform user about the "Sending..." state (interval … POST requestFor POST requests, in order to avoid "Forbidden (CSRF … which result in hidden variable: that we read in javascript: orrendering csrf in views.py by: which result in: rendered in template that we read in javascript: Finally we could set: POST - ing data and file(s) with FormData

Introduction POST

July 22, 2020

POST requests are almost identical structured as GET by using … and setting csrf token required by Django.Also, to send key …

Python: using time functions

July 17, 2020

Reading UTC (Coordinated Universal Time aka Greenwich Mean Time - … as struct_time structure: as datetime structure: Our absolute reference system time is UTC (GMT). In order … Reading local time as struct_time: Reading UTC time as naive datetime: Reading local time as naive datetime: Reading UTC time as aware datetime (offset = 0): Reading local time as aware datetime: Conversions time_struct to/from epochseconds and formatted output: result: Reading different local times as time_struct: result: Converting date/time values between different zonesThe preferred way of dealing … result:

UDP python sockets programming

July 17, 2020

After binding a port number to server listening socket, udp … Udp protocol can deliver broadcast messages to all addresses of … With non-blocking sockets, we could manage multiple sockets in the …