标签:
STATIC_URL = ‘/static/‘
STATICFILES_DIRS = (
os.path.join(BASE_DIR, ‘static‘),
)
<script src="/static/xxx.js"></script>
To design URLs for an application, you create a Python module called a URLconf. Like a table of contents for your app, it contains a simple mapping between URL patterns and your views.
urls.py
from django.conf.urls import url
from cmdb import views
urlpatterns = [
url(r‘^index/‘, views.index), # mapping URL patterns and views
]
views.py
a view is responsible for doing some arbitrary logic, and then returning a response.
from django.shortcuts import HttpResponse
def index(request):
return HttpResponse(‘hello world‘)
With Django’s template system, we can separate the design of the page from the Python code itself. A Django template is a string of text that is intended to separate the presentation of a document from its data. Usually, templates are used for producing HTML, but Django templates are equally capable of generating any text-based format.
{% for item in item_list %}
<li>{{ item }}</li>
{% endfor %}
if tag:
{% if ordered_warranty %}
<p>Your warranty information will be included in the packaging.</p>
{% else %}
<p>You didn‘t order a warranty, so you‘re on your own when
the products inevitably stop working.</p>
{% endif %}
filter
<p>Thanks for placing an order from {{ company }}. It‘s scheduled to ship on {{ ship_date|date:"F j, Y" }}.</p>
In this example, {{ ship_date|date:"F j, Y" }}, we’re passing the ship_date variable to the date filter, giving the date filter the argument "F j, Y".
Here is the most basic way you can use Django’s template system in Python code:
from django.shortcuts import render
USER_INPUT = {}
def index(request):
"""get submit"""
if (request.method == ‘post‘):
user = request.POST.get(‘user‘, None) # None is default value
email = request.POST.get(‘email‘, None)
temp = {‘user‘: user, ‘email‘: email}
USER_INPUT.append(temp)
"""
template loading: t = template.Template(‘My name is {{ name }}.‘)
context creation: c = template.Context({‘name‘: ‘Adrian‘})
template rendering: t.render(c)
HttpResponse creation
"""
return render(request, ‘index.html‘, {‘data‘: USER_INPUT})
render()
Django provides a shortcut that lets you
1. load a template 加载模板
2. render it and return an HttpResponse 渲染
all in one line of code.
Django’s database layer.
models.py
from django.db import models
# Create your models here.
class UserInfo(models.Model):
user = models.CharField(max_length=32)
email = models.CharField(max_length=32)
create databases:
python manage.py makemigrations
python manage.py migrate
509 garyyang:mysite_django$ python3 manage.py makemigrations
Migrations for ‘cmdb‘:
cmdb/migrations/0001_initial.py:
- Create model UserInfo
511 garyyang:mysite_django$ python3 manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, cmdb, contenttypes, sessions
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying cmdb.0001_initial... OK
Applying sessions.0001_initial... OK
标签:
原文地址:http://www.cnblogs.com/garyang/p/5858556.html