Beginners Guide For Django(Web Framework)
Django is a free and open-source web framework, written in Python, which follows the model-view-template (MVT) architectural pattern. It is maintained by the Django Software Foundation (DSF). Django's primary goal is to ease the creation of complex, database-driven websites. Django emphasizes reusability and "pluggability" of components, rapid development, and the principle of Don't Repeat Yourself(DRY). Django is available under the BSD license. The source code repository is stored on GitHub.
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source. Django was designed to help developers take applications from concept to completion as quickly as possible. Django takes security seriously and helps developers avoid many common security mistakes. Some of the busiest sites on the Web leverage Django’s ability to quickly and flexibly scale.
Django Installation Steps:
1. Get Python installed.
2. Install Pip.
3. Open Terminal and type > sudo pip install django OR Follow This.
4. Check Django version to see if its installed correctly. Type > django-admin --version
Creating First Project in Django
1. Create a directory where you will keep all of your Django Projects.
2. cd to that directory and type > django-admin startproject MySite (here MySite is project name). MySite directory is just a container for your project.
Now. your directory structure will be like
MySite/
manage.py
MySite/
__init__.py
settings.py
urls.py
wsgi.py
3. Now Start an app inside the project directory by typing > python manage.py startapp myapp(this command will create the directory of the app with name myapp)
Directory structure for myapp will be like
myapp/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py
__init__.py file tells Python that this directory is be considered as a Python package. This is an empty file.
4. To see something on the web browser open up the views.py file in any code editor(i will suggest you PyCharm) and write the below code in it.
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, Welcome to Django. You created your first app successfully, congratulations to you.")
5. Now open up the urls.py file in MySite directory and write the below code in it.
from django.conf.urls import url
from django.contrib import admin
from polls import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.index, name='index'),
]
6. Now to run the server type > python manage.py runserver(this command will start the server and gives you the url of your app like "Starting development server at http://127.0.0.1:8000/") click on the url and you will see your apps first page.
Note: after running python manage.py runserver you might get something on terminal which will say the you have few unapplied migration(s). Your project may not work properly until you apply the migrations for app(s). Run 'python manage.py migrate' to apply them. So ignore that for now you can learn that later when you will write some advanced apps for projects. Because it is related to all database creation and tables creation.
7. You can also runserver on different ip and port by typing
python manage.py runserver 0.0.0.0:8888 OR python manage.py runserver 8080
Here i have an app, you can see it. This app has the task of uploading and downloading files, and saving the data into MySQL database.
Comments
Post a Comment