Django project example analysis

The main route is:
add the template first, then register it in the view, and then add the mapping of the url path in URLS.

Template one:

<!--base.html--> 
<h1 style="margin:auto;width:40%">Open positions at Lanzhu Technology</h1> < 
!--Definition title: Open positions at Lanzhu Technology, displayed in the center and Occupies 50% of the page width--> 
<p></p> 
<!--#Use the block directive to define the block. The name of the block is content--> 
{% block content %} 
{% endblock %}

Template two:

<!--If you want to display html, you must first go to the view layer and add the custom page--> < 
!--Inherit the base page--> 
{% extends 'base.html' %} 
<!--The following The content content will replace the content content in the base --> 
<!--The fastest way to comment content in html, position the cursor on the line to be commented, press ctrl+/--> 
{% block content %} 
    Finally wait for you , looking forward to joining us and using technology to explore a new world 
{% if job_list %} 
<!--The above means to determine whether job_list has content, and if so, execute the following for loop--> < 
!--job_list is from Passed from the view layer--> 
<!--ul tag to define a list, each line is a position, href is a link, the link points to the URL of the job details--> 
    <ul> 
<!--Start the for loop-- > 
        {% for jobs in job_list %} 
        <li>{ 
  
  {jobs.job_type}}
  
  <a href="/jobs/{ {jobs.id}}/" style="color:blue">
   { 
  {jobs.job_name} }</a> {
   
  {jobs.job_city}}</li>
        {% endfor%} 
<!--End the for loop-->
    </ul>
{% else %}
{% endif %}
{% endblock %}

Template three:

{% extends 'base.html' %} 
{% block content %} 
The <!--<div> tag defines a separated block or a region in an HTML document. --> 
<div style="margin:auto;width:50%"> 
<!-- The <a> tag defines a hyperlink that is used to link from one page to another. --> 
<!-- The most important attribute of the <a> element is the href attribute, which specifies the target of the link. --> 
    <a href="/joblist" style="color:blue">Return to job list</a> 
{% if jobs %} 
<!-- class= in html is a selector and can be understood as a Logo, used to identify specific tags --> 
    <div class="position_name"> 
        <h2>Position name: { 
  
  {jobs.job_name}}</h2> 
        City: 
        { 
  
  {jobs.job_city}} 
    </div>< br> 
    <! -- The <hr> tag defines topic changes (such as topic changes) in HTML pages and is displayed as a horizontal line. The <hr> element is used to separate content in an HTML page (or to define a variation). In HTML, the <hr> tag has no closing tag. -->
    --> <hr>
    <div class="position_responsibility" style="width:600px;"> 
        <h3>Job Responsibilities:</h3> 
<!-- <pre> Tags can define pre-formatted text. Text enclosed in <pre> tag elements usually preserves spaces and newlines. The text will also be rendered as a fixed-width font. --> 
        <pre style="font-size:16px">{ 
  
  {jobs.job_responsibility}} 
        </pre> 
        <p></p> 
<!-- The <br> tag inserts a simple newline. The <br> tag is an empty tag, meaning it has no closing tag. There will be a larger blank area below this block--> 
    </div><br> 
    <hr> 
    <div class="position_requirement" style="width:600px;"> 
        <h3>Position Requirements</h3> 
        <pre style="font-size:16px">{ 
  
  {jobs.
        <input type="button" style="width:120px;background-color:lightblue;" value="Apply"/> 

    </div> 

{% else %} 
<!-- <p> Tag definition paragraph. --> 
<!-- The <p> element automatically creates some white space before and after it. The browser adds these spaces automatically, or you can specify them in your stylesheet. That is, smaller blank lines --> 
    <p>Position does not exist</p> 
{% endif %} 
{% endblock %} 
</div>

job application views views.py

from django.shortcuts import render 
from django.template import loader 
from job.models import work 
from job.models import JobTypes,Cities 
from django.http import HttpResponse 
from django.http import Http404 
# Create your views here. 
#1. Use the function to The html page is defined at the view level 
def joblist(request): 
    job_list=work.objects.order_by('job_type')#work.objects.order_by is the syntax in django modle. You can directly take out the work job list and sort the 
    template according to the job type. =loader.get_template('joblist.html')#Use the loader to load the template in 
    context={'job_list':job_list}#Define a context 
    for jobs in job_list: 
        jobs.job_city=Cities[jobs.job_city][1] 
        jobs.job_type=JobTypes[jobs.job_type][1] 
    return HttpResponse(template.render(context)) 
#Don’t forget to define the joblist view in the url path
def detail(request,jobs_id):
    try:
        jobs=work.objects.get(pk=jobs_id)
        jobs.job_city=Cities[jobs.job_city][1]
    except work.DoesNotExist:
        raise Http404("jobs does not exist")
    return render(request,'job.html',{'jobs':jobs})

job application urls.py

from django.conf.urls import url 
from job import views 
urlpatterns=[ 
    #Job list 
    url(r"^joblist/",views.joblist,name="joblist"), 
    url(r"^jobs/(?P<jobs_id >\d+)/$",views.detail,name="detail")#The hyperlink path defined in joblist.html passes jobs_id as a parameter to views.detail 
]

Main application settingspy

INSTALLED_APPS = [
    'job',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

Main application url.py

from django.contrib import admin 
from django.urls import path 
from django.conf.urls import include,url 

urlpatterns = [ 
    url(r"^",include("job.urls")),#Include job.urls through include The paths are mapped to 
    path('admin/', admin.site.urls), 
]

Achievements display

Guess you like

Origin blog.csdn.net/qq_40333984/article/details/126228693