About me

Saturday, May 12, 2018

learn python django chap 1

                                       Simple Website Using django

                                       django have a default web server it also restarts whenever we modify the code. apache and lighttpd  like servers can also be used

                        At first By running this command we can know if django is installed
 
               
python -m django --version


if it is installed you will see the version of the django other wise you can install it in the site
See How to install Django for advice on how to remove older versions of Django and install a newer one.

To start the project you first need to create the initial setup for the project by auto generating some code that establishes the django project for that we need to get first change the directory in command line to our desired place by using cd command then we will put the code as

          $ django-admin startproject pysite
 
this will create a pysite directory in our current directory if it didn't work we can search here
this will create a project structure as given below
 
 
pysite/
    manage.py
    pysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py
 
pysite is just directory container
manage.py : This file is kind of your project local django-admin for interacting

with your project via command line  To get a full list of command accessible via
manage.py you can use the code given below. manage.py is automatically created in 
each Django project. manage.py does the same thing as django-admin but takes care
 of a few things for you
  •  It puts your project’s package on sys.path.                   
  •  It sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file.

                           
$ python manage.py help

 
 
the pysite subfolder is the actual python package for the python project
 Its name is the Python package name you’ll need to use to import anything inside it (e.g. pysite.urls). it contains four files

  •  __init__.py − Just for python, treat this folder as package 

  •  settings.py − As the name indicates, your project settings.
  •  urls.py −The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs in URL_dispatcher
  •  wsgi.py − An entry-point for WSGI-compatible web servers to serve your project. See How to deploy with wsgi for more details.

 to verify that your django projects works. change into the outer pysite directory

 and you can run the command


 
                   
                      

$ python manage.py runserver
 
 we will get the following output 
 
 
 
Performing system checks... System check identified no issues (0 silenced). You have unapplied migrations; your app may not work properly until they are applied. Run 'python manage.py migrate' to apply them. April 24, 2018 - 15:50:53 Django version 2.0, using settings 'pysite.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
        
  
ignore the unapplied database migrations error the database will be explained later
You’ve started the Django development server, a lightweight Web server written
 purely in Python. its included with Django so you can develop things rapidly,
 without having to deal with configuring a production server – such as Apache
 – until you’re ready for production.

Now’s a good time to note: don’t use this server in anything resembling a production environment. It’s intended only for use while developing.
Now that the server’s running, visit http://127.0.0.1:8000/ with your Web browser. You’ll see a “Congratulations!” page, with a rocket taking off. It worked!
 
 
 Now Create the Project
 
  Your apps can live anywhere on your python_path. In this tutorial, we’ll create our myproj app right next to your manage.py file so that it can be imported as its own top-level module, rather than a submodule of pysite.

To create your app, make sure you’re in the same directory as manage.py and type this command:
                     
                   
$ python manage.py startapp myproj
 
  That’ll create a directory myproj, which is laid out like this:
 myproj/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    views.py
 
 This directory structure will house the application.
  • __init__.py − Just to make sure python handles this folder as a package.
  • admin.py − This file helps you make the app modifiable in the admin interface.
  • models.py − This is where all the application models are stored.
  • tests.py − This is where your unit tests are.
  • views.py − This is where your application views are.
 
 Now We can Create Our First View
  open the myproj folder views.py file and write this code
 
  from django.http import HttpResponse
def index(request):
    return HttpResponse("Hello, world. You're at django.")


This is the simplest view possible in Django. To call the view, we need to map it to a URL - and for this we need a URLconf.
To create a URLconf in the myproj directory, create a file called urls.py. Your app directory should now look like:
 
 myproj/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    urls.py
    views.py
 
 In the myproj/urls.py file include the following code:
  
from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), ]
  The next step is to point the root URLconf at the myproj.urls module. In pysite/urls.py, add an import for django.urls.include and insert an include() in the urlpatterns list, so you have:
 
 
 
 
 from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('myproj/', include('myproj.urls')),
    path('admin/', admin.site.urls),
]
 
 
The iclude() function allows referencing other URLconfs. Whenever Django encounters include() it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.
The idea behind include() is to make it easy to plug-and-play URLs. Since myproj are in their own URLconf (myproj/urls.py), they can be placed under “/myproj/”, or under “/fun_myproj/”, or under “/content/myproj/”, or any other path root, and the app will still work.
 
When to use include()
You should always use include() when you include other URL patterns. admin.site.urls is the only exception to this.
 
You have now wired an index view into the URLconf. Lets verify it’s working, run the following command:
 
  $python manage.py runserver 
 
 
Go to http://localhost:8000/myproj/ in your browser, and you should see the text “Hello, world. You’re at django .”, which you defined in the index view.
The path() function is passed four arguments, two required: route and view, and two optional: kwargs, and name. At this point, it’s worth reviewing what these arguments are for.

path() argument: route

                             route is a string that contains a URL pattern . When processing a request , Django starts at the first pattern in urlpatterns and makes its way down the list comparing the requested URL against each pattern until it finds one that matches                    

                                  Patterns don’t search GET and POST parameters, or the domain name. For example, in a request to https://www.example.com/myapp/, the URLconf will look for myapp/. In a request to https://www.example.com/myapp/?page=3, the URLconf will also look for myapp/.
 

  path() argument: view

         When Django finds a matching pattern, it calls the specified view function with an HttpRequest object as the first argument and any “captured” values from the route as keyword arguments. We’ll give an example of this in a bit. 
 

path() argument: kwargs

        
         Arbitrary keyword arguments can be passed in a dictionary to the target view. We aren’t going to use this feature of Django in the tutorial

path() argument:name

 
                            Naming your URL lets you refer to it unambiguously from elsewhere in Django, especially from within templates. This powerful feature allows you to make global changes to the URL patterns of your project while only touching a single file.




THANKS FOR VIEWING MY BLOG NEXT POST IS ON "SETUP DATABASE" HOPE YOU LIKE THIS BLOG PLEASE COMMENT YOUR SUGGESTIONS AND SHARE THIS BLOG TO YOUR FRIENDS AND DON'T FORGET TO FOLLOW THIS BLOG

2 comments:

  1. The Original Forex Trading System: roboforex login Is The Original Forex Trading System. It Is 100% Automated And Provides An Easy-to-follow Trading System. You Get Access To Real-time Signals, Proven Methods, And A Money-back Guarantee.

    ReplyDelete