장고 템플릿 언어
All the steps should be finished before start. Refer to: [Ultimate] Django Web created by Command
1. render(request, x.html, context)
1.1 views.py
def index(request):
context = {
'title':'caseid',
'content':'company',
}
return render(request, 'index.html', context)
1.2 index.html
<div class="header">
</div>
<div class="description">
<p>
<blockquote>
<p>This is an updated approach to create Django project by Pycharm community version which is much easier and more reasonable than the former one: <a href="http://davidkor.logdown.com/posts/4716406"> Django Web created by Pycharm+Command</a></p>
</blockquote>
<h1 id="1pycharm---new-project">1.Pycharm - new project</h1>
<p><img src="http://user-image.logdown.io/user/42937/blog/39533/post/5298745/jRV2IJYmSQqJb9zZEICB_1.png" alt="1.png" /></p>
<p>we don’t use pycharm to create a virtual environment.</p>
<h1 id="2command">2.Command</h1>
<p>$<code class="highlighter-rouge">cd PycharmProjects</code></p>
<p>$<code class="highlighter-rouge">cd 180124_3</code></p>
<p>$<code class="highlighter-rouge">python3 -m venv my_venv</code></p>
<p>$<code class="highlighter-rouge">cd my_venv</code></p>
<p>$<code class="highlighter-rouge">source bin/activate</code></p>
<p>$<code class="highlighter-rouge">pip install django</code></p>
<p>$<code class="highlighter-rouge">django-admin.py startproject my_project</code></p>
<p>$<code class="highlighter-rouge">cd my_project</code></p>
<p>$<code class="highlighter-rouge">python manage.py migrate</code></p>
<p>$<code class="highlighter-rouge">python manage.py runserver</code></p>
<p><img src="http://user-image.logdown.io/user/42937/blog/39533/post/5298745/53qZkHTJ2SBSr8BvquAg_2.png" alt="2.png" /></p>
<h1 id="3run-1270018000">3.Run 127.0.0.1:8000</h1>
<p><img src="http://user-image.logdown.io/user/42937/blog/39533/post/5298745/PvTmLclmRU2j92agJ1KR_3.png" alt="3.png" /></p>
<h1 id="4python-managepy-startapp-django_web">4.python manage.py startapp django_web</h1>
<p>$<code class="highlighter-rouge">python manage.py startapp django_web</code></p>
<h1 id="5add-django_web-to-installed-app">5.Add django_web to installed app</h1>
<p>my_project/settings.py</p>
<p><img src="http://user-image.logdown.io/user/42937/blog/39533/post/5298745/JdT46TmIQPWt6dP2Jtsi_4.png" alt="4.png" /></p>
<h1 id="6mkdir-templates-under-django_web">6.mkdir templates under django_web</h1>
<p>mkdir templates under django_web
copy index.html to templates folder</p>
<h1 id="7viewspy">7.views.py</h1>
<p>django_web/views.py</p>
<pre><code class="language-Python">def index(request):
return render(request, 'index.html')
</code></pre>
<p><img src="http://user-image.logdown.io/user/42937/blog/39533/post/5298745/LoRWZnGeTSit0iaefvQA_5.png" alt="5.png" /></p>
<h1 id="8urlspy">8.urls.py</h1>
<p>my_project/urls.py</p>
<pre><code class="language-Python">
from django_web.views import index
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', index),
]
</code></pre>
<p><img src="http://user-image.logdown.io/user/42937/blog/39533/post/5298745/C3eO5JciS5GDhuzIxuFK_6.png" alt="6.png" /></p>
<h1 id="9get-access-to-1270018000index">9.Get access to 127.0.0.1:8000/index</h1>
<p><img src="http://user-image.logdown.io/user/42937/blog/39533/post/5298745/Z08OaWjpTt2qK3CYwr34_7.png" alt="7.png" /></p>
<p>it can run normally except css. then we will continue to add css to index.html</p>
<h1 id="10static">10.”static”</h1>
<p>$<code class="highlighter-rouge">mkdir static</code>, copy ‘css’ and ‘images’folder to “static”</p>
<p>go to templates/index.html, put the following code on the 1st line of this html file</p>
<pre><code class="language-Python">
{% load static %}
</code></pre>
<p>change all the css styles and image paths by using <code class="highlighter-rouge">{% static 'oringin path' %}</code>,sample as follows</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
<link rel="stylesheet" type="text/css" href="{% static 'css/semantic.css' %}">
<img src="{% static 'images/0001.jpg' %}" width="100" height="91">
</code></pre></div></div>
<p><img src="http://user-image.logdown.io/user/42937/blog/39533/post/5298745/L90aIjAHSrGWwzv7U2YX_1.png" alt="1.png" /></p>
<p>but, the “static files” may not be found by codes, so we need to set the absolute path in settings.py</p>
<pre><code class="language-Python">
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
</code></pre>
<p><img src="http://user-image.logdown.io/user/42937/blog/39533/post/5298745/zSW8jhhR0mNfUcmOJrYR_2.png" alt="2.png" /></p>
<p>Done, html can run with css and give up a wonderful page!!</p>
<p><img src="http://user-image.logdown.io/user/42937/blog/39533/post/5298745/zrK26yiTKiYOeuLKod5A_3.png" alt="3.png" /></p>
</p>
</div>
2. Connect to Mongodb
Django has integrated with sqlite3, however, for easy data sorting, we need to import mongoengine.
2.1 settings.py
Django will run settings.py first to check all the configrations.
from mongoengine import connect
connect('awms', host='127.0.0.1', port=27017)
2.2 models.py
from mongoengine import *
from mongoengine import connect
connect('awms', host='127.0.0.1', port=27017)
class LogInfo(Document):
caseid = StringField()
orgnization = StringField()
meta = {'collection':'loginfo_backup'}
for i in LogInfo.objects[:1]:
print(i.caseid)
PROBLEM-1:mongoengine module package need to be installed
Pycharm - Files - Default Settings - Project Interpreter - + - mongoengine - install
obviously, it is the easy and truly correct way to install mongoengine package, BUT when run models.py, error occurred ‘ModuleNotFoundError: No module named ‘mongoengine’’
ANSWER
Go to the project root path use command to install mongoengine package.
$ ~/PycharmProjects/180125/my_project ⮀ pip install mongoengine
Donno why ㅠㅜ
PROBLEM-2:mongoengine.errors.FieldDoesNotExist
the fields “{‘note’,…}” do not exist on the document “LogInfo”
ANSWER
All the columns in db should be defined here.
2.2’ models.py
from mongoengine import *
from mongoengine import connect
connect('awms', host='127.0.0.1', port=27017)
class LogInfo(Document):
caseid = StringField()
problem = StringField()
custtpye = StringField()
orgnization = StringField()
handle_by = StringField()
city = StringField()
no = StringField()
date = StringField()
attachment = StringField()
solution = StringField()
source = StringField()
resolved = StringField()
note = StringField()
meta ={'collection':'loginfo_backup'}
for i in LogInfo.objects[:1]:
print(i.caseid, i.date)
If the database already exits mata={}
, otherwise no need.
2.3 views.py
from django.shortcuts import render
from django_web.models import LogInfo
def index(request):
log_info = LogInfo.objects[:1]
context = {
'title': log_info[0].caseid,
'content':log_info[0].orgnization,
}
return render(request, 'index.html', context)
#2.4 Paginator Firstly, get to know how paginator works.
views.py - console
$ from django.core.paginator import Paginator
$ iter = 'qwertyuiop'
$ paginator = Paginator(iter,3)
$ paginator.page(1)
<Page 1 of 4>
$ page1 = paginator.page(1)
$ page1.object_list
'qwe'
$ page1.has_next()
True
$ page1.number
1
$ page1.paginator.num_pages
4
Let’s jump into codes.
- views.py
from django.shortcuts import render
from django_web.models import LogInfo
from django.core.paginator import Paginator
def index(request):
limit = 10
log_info = LogInfo.objects[:10]
paginator = Paginator(log_info, limit)
page = request.GET.get('page', 1)
loaded = paginator.page(page)
context = {
'LogInfo':loaded
}
return render(request, 'index.html', context)
- index.html
<div class="ui divided items">
</div>
- Activate the paginator
A sample of how the paginator looks like.
<a> 《 Previous </a>
<span> 1 of 4 </span>
<a> Next 》 </a>
Then use template language to activate it.
<div class="ui small pagination menu">
<div class="disabled item">
of
</div>
</div>
What does
page = request.GET.get('page', 1)
really mean?
Due to the video,
[:10]
should be removed inlog_info = LogInfo.objects[:10]
cuz it means only find 10 items BUT, even it wasn’t deleted, the web page could get data well Donno why????