Skip to main content

Posts

Showing posts from January, 2022

Template System for Django

 Syntax 1. Variable {{}} e.g. <html><body>{{ now }}</body></html> 2. for loop {% for x in list %} {% endfor %} e.g. <ul>      {% for item in list %}           <li>{{ item }}</li>      {% endfor %} </ul> 3. if else {% if %} {% else %} {% endif %} e.g. {% if true %}      <p> it is a true part</p> {% else %}      <p> it is a false part</p> {% endif %} This is what we should write in html file. However, we should define a function to pass these variables. We should define a function in views.py def get_index_page (request): all_article = Article.objects.all() return render(request , 'app1/index.html' , { 'article_list' : all_article }) After that, we should set content in urls.py path( 'index' , app1.views.get_index_page)

Django Note

Environment: Python3.5+ Conda Django 2.0     pip install django==2.0 Commands to Start a Project 1. Create a project: sudo django-admin startproject xxx 2. Run the server: sudo python3 manage.py runserver 0:8000 Then we can open  http://127.0.0.1:8000/  to see the website 3. Create a app in project sudo python3 manage.py startapp app1 Different Usages of Files 1. views.py Deal with visible things 2. models.py Define the models 3. admin.py Define Admin module 4. apps.py Defile the apps 5. tests.py place to write test cases 6. urls.py (We create ourselves) manage routers Url Address: http://127.0.0.1:8000/app1/hello_world Create Models - Update defined date to database Defile models in models.py: class Article(models.Model): # ID article_id = models.AutoField( primary_key = True ) # Title title = models.TextField() # Abstract abstract = models.TextField() # Date pub_date = models.DateTimeField( auto_now = True ) # Content content...