Web Applications using Python and Django

The render function will be used in the next section.Now, create a new Route inside the urls.py file of the project folder (webapp/urls.py), to direct request for the root site to your index() function.from example import viewsurlpatterns = [ path('admin/', admin.site.urls), path('', views.index)]The first line imports the views.py from the current application folder (example), as views object in the current context.Then, you can specify a new route path ‘’ (root path) in urlpatterns, mapped to respond with the result of views.index (views.py, function index())Now, when you run your server (python manage.py runserver) you can view your new home page in http://localhost:8000/.Hello WorldThis is a simple page..In the next section you can create full HTML template to show as your home page.The settings.py fileWhen you open the file webapp/settings.py, you will find different settings to configure your main project..The most important ones to review are:ALLOWED_HOSTS: A list of allowed hosts that can connect to your application..For now, it will keep empty.INSTALLED_APPS: A list of imports that will be loaded with your application..In this part, you can add an item for the PollsConfig object in example/apps.py:INSTALLED_APPS = [ ‘django.contrib.admin’, ‘django.contrib.auth’, ‘django.contrib.contenttypes’, ‘django.contrib.sessions’, ‘django.contrib.messages’, ‘django.contrib.staticfiles’, ‘example.apps.ExampleConfig`, ]MIDDLEWARE: A list of middlewares that run with your application..A middleware is an external layer of the application to process the information received by your application before it’s available to process:SessionsAuthenticationCSFR token validationDATABASES: A list of databases with their connection parameters and configuration..In this project, one SQLite database is used..The most commonly used are django.db.backends.sqlite3, django.db.backends.postgresql, django.db.backends.mysql, and django.db.backends.oracleConclusionIn this post we created a Python web application with Django following this commands:Create the main web server project and start running the service:Run django-admin startproject webappExecute cd webapp to change to the project folderRun python manage.py migrateExecutepython manage.py createsuperuserFinally, runpython manage.py runserverCreate an application inside the project:Runpython manage.py startapp example to create the application folderEdit the file example/views.py to create an index() functionAdd inwebapp/urls.py a new route path in urlpatternsModify webapp/settings.py to add the application PollsConfig (example.apps.ExampleConfig). More details

Leave a Reply