Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

بررسی چارچوب جنگو

214 views

Published on

ارائه توسط استارتاپ اسپرتاپ

Published in: Education
  • Be the first to comment

  • Be the first to like this

بررسی چارچوب جنگو

  1. 1. Python Web Framework
  2. 2. 2 Django?
  3. 3. 3 Django?
  4. 4. 4 Django  A high-level Python web framework  Created in 2003, open sourced in 2005  Encourages rapid development and clean, pragmatic design  Widely supported, many deployment options  Focus on automation and DRY  “For perfectionists with deadlines”  MVC (MTV) Architecture
  5. 5. 5 Who uses Django?  BitBucket  DISQUS (serving 400 million people)  Pinterest  Instagram  dPaste  Mozilla (support.mozill, addons.mozilla)  NASA  PBS (Public Broadcasting Service)  The Washington Post, NY Times, LA Times, The Guardian  National Geographic, Discovery Channel
  6. 6. 6 Features  Object Relational Mapper - ORM  Template System  customizable Admin Interface, makes CRUD easy  Built-in light weight Web Server  URL design  Authentication / Authorization  Internationalization support  Cache framework, with multiple cache mechanisms  Free, and Great Documentation
  7. 7. MVC vs. MTV M Model V View C Controller M Model T Template V View
  8. 8. 8 Installation  Prerequisites  Python  PIP for installing Python packages  pip install Django  pip install mysql-python  Testing installation  import django  Create a requirements.txt  $ pip install -r requirements.txt
  9. 9. 9 Virtualenv  pip “one project, one virtualenv”  projects with different dependencies, package versions  easier to deploy. Forget dependency hell!
  10. 10. 10 Django Architecture Models Describes your data Views Controls what users sees Templates How user sees it Controller URL dispatcher DataBase Model View Template URL dispatcher Brower
  11. 11. 11 Project Creation  Creating new Project  django-admin.py startproject demoproject A project is a collection of applications  Creating new Application  python manage.py startapp demosite An application tries to provide a single, relatively self-contained set of related functions  Using the built-in web server  python manage.py runserver Runs by default at port 8000
  12. 12. 12 Project Directory Structure demosite/ manage.py. demosite/ __init__.py settings.py urls.py wsgi.py templates/ static/ demoapp/ __init__.py urls.py views.py models.py admin.py forms.py
  13. 13. 13 Apps  use short, obvious, single-word names for your apps  many small apps is better than a few giant apps:  explain an app in a sentence. If you can't, split the app  rather than expand an app, write a new app  don't reinvent the wheel!  django.contrib  3rd-party apps
  14. 14. 14 Settings  use - multiple settings files: - per environment:  dev, testing, staging, production - per developer (local settings)  all settings files must inherit from base, so you can do:  INSTALLED_APPS += ('debug_toolbar', ) - version control all the settings!
  15. 15. 15 Models you can use Django without a database Or describe your database layout in Python code with django ORM
  16. 16. 16 Models from django.db import models class Reporter(models.Model): full_name = models.CharField(max_length=70) def __str__(self): # __unicode__ on Python 2 return self.full_name class Article(models.Model): pub_date = models.DateField() headline = models.CharField(max_length=200) content = models.TextField() reporter = models.ForeignKey(Reporter) def __str__(self): # __unicode__ on Python 2 return self.headline
  17. 17. 17 admin from django.contrib import admin from . import models admin.site.register(models.Article)
  18. 18. 18 URLs  Map URLs in requests to code that can be executed  Regular expressions!  Subsections of your site can have their own urls.py modules
  19. 19. 19 URLs  Root URL should be configured in settings.py o ROOT_URLCONF = 'app.urls'  Example urlpatterns = patterns(' ', (r'^articles-year/$', 'mysite.news.views.articles_year'), )
  20. 20. 20 Views  Map Code that handles requests  Other frameworks often call these “controllers”  Basically a function that:  gets a request  returns text or a response
  21. 21. 21 Views from django.shortcuts import render from .models import Article def year_archive(request, year): a_list = Article.objects.filter(pub_date__year=year) context = {'year': year, 'article_list': a_list} return render(request, 'news/year_archive.html', context)
  22. 22. 22 Django Template Language  Call a function or do logic: {% ... %}  Variable substitution: {{ bar }}  Filters: {{ foo|bar }}
  23. 23. 23 Django Template Language {% extends "base.html" %} {% block title %}Articles for {{ year }}{% endblock %} {% block content %} <h1>Articles for {{ year }}</h1> {% for article in article_list %} <p>{{ article.headline }}</p> <p>By {{ article.reporter.full_name }}</p> <p>Published {{ article.pub_date|date:"F j, Y" }}</p> {% endfor %} {% endblock %}
  24. 24. 24 Django Template Language (base) {% load staticfiles %} <html> <head> <title>{% block title %}{% endblock %}</title> </head> <body> <img src="{% static "images/sitelogo.png" %}" alt="Logo" /> {% block content %}{% endblock %} </body> </html>
  25. 25. 25 Form from django import forms class MyForm(forms.Form): name = forms.CharField(max_length=30) email = forms.EmailField()
  26. 26. 26 Using a Form in a View from myapp.forms import MyForm def my_view(request): form = MyForm(request.POST or None) if request.method == "POST" and form.is_valid(): name = form.cleaned_data['name'] email = form.cleaned_data['email'] # do something great with that data return render(request, 'myapp/myform.html', { 'form': form })
  27. 27. 27 Forms in Templates <html> ... <body> <form action="{% url 'myapp:my_form' %}" method="post"> {% csrf_token %} {{ form.as_p }} <button type="submit">Go!</button> </form> </body> </html>
  28. 28. 28 Django-rest-framework Django REST framework is a powerful and flexible toolkit for building Web APIs.
  29. 29. 29 Django-rest-framework (installation)  pip install djangorestframework  pip install django-rest-swagger  Pip install django-filter
  30. 30. 30 Django-rest-framework (installation) # settings.py INSTALLED_APPS = ( ... ‘rest_framework’, )

×