Chapter 1: Getting started with Django


Download 0.85 Mb.
Pdf ko'rish
bet3/5
Sana04.09.2020
Hajmi0.85 Mb.
#128416
1   2   3   4   5
Bog'liq
Django

{{post.title}}

 

           
Published on {{post.date_published}}
 

           


Written by: {{post.author}}
 

           


Permalink
 

           


{{post.body}}
 

       

 

    {% endfor %} 



Extending your templates

Context processor to determine the template based on group membership(or any query/logic). 

This allows our public/regular users to get one template and our special group to get a different 

one.


myapp/context_processors.py

def template_selection(request): 

    site_template = 'template_public.html' 

    if request.user.is_authenticated(): 

        if request.user.groups.filter(name="some_group_name").exists(): 

            site_template = 'template_new.html' 

 

    return { 



        'site_template': site_template, 

    }


Add the context processor to your settings.

In your templates, use the variable defined in the context processor.

{% extends site_template %}

Read Context Processors online: 

https://riptutorial.com/django/topic/491/context-processors

https://riptutorial.com/

34


Chapter 8: Continuous Integration With 

Jenkins

Examples

Jenkins 2.0+ Pipeline Script

Modern versions of Jenkins (version 2.x) come with a "Build Pipeline Plugin" that can be used to 

orchestrate complex CI tasks without creating a multitude of interconnected jobs, and allow you to 

easily version-control your build / test configuration.

You may install this manually in a "Pipeline" type job, or, if your project is hosted on Github, you 

may use the "GitHub Organization Folder Plugin" to automatically set up jobs for you.

Here's a simple configuration for Django sites that require only the site's specified python modules 

to be installed.

#!/usr/bin/groovy 

 

node { 



  // If you are having issues with your project not getting updated

  // try uncommenting the following lines. 

  //stage 'Checkout' 

  //checkout scm 

  //sh 'git submodule update --init --recursive' 

 

  stage 'Update Python Modules' 



  // Create a virtualenv in this folder, and install or upgrade packages 

  // specified in requirements.txt; https://pip.readthedocs.io/en/1.1/requirements.html 

  sh 'virtualenv env && source env/bin/activate && pip install --upgrade -r requirements.txt' 

 

  stage 'Test' 



  // Invoke Django's tests 

  sh 'source env/bin/activate && python ./manage.py runtests' 

}

Jenkins 2.0+ Pipeline Script, Docker Containers

Here is an example of a pipeline script that builds a Docker container, then runs the tests inside of 

it. The entrypoint is assumed to be either 

manage.py

 or 

invoke


/

fabric


 with a 

runtests


 command 

available.

#!/usr/bin/groovy 

 

node { 



  stage 'Checkout' 

  checkout scm 

  sh 'git submodule update --init --recursive' 

 

  imageName = 'mycontainer:build' 



https://riptutorial.com/

35


  remotes = [ 

    'dockerhub-account', 

  ] 

 

  stage 'Build' 



  def djangoImage = docker.build imageName 

 

  stage 'Run Tests' 



  djangoImage.run('', 'runtests') 

 

  stage 'Push' 



  for (int i = 0; i < remotes.size(); i++) { 

      sh "docker tag ${imageName} ${remotes[i]}/${imageName}" 

      sh "docker push ${remotes[i]}/${imageName}" 

   } 


}

Read Continuous Integration With Jenkins online: 

https://riptutorial.com/django/topic/5873/continuous-integration-with-jenkins

https://riptutorial.com/

36


Chapter 9: CRUD in Django

Examples

**Simplest CRUD example**

If you find these steps unfamiliar, consider starting 

here instead

. Note these steps come from 

Stack Overflow Documentation.

django-admin startproject myproject 

cd myproject 

python manage.py startapp myapp



myproject/settings.py Install the app

INSTALLED_APPS = [ 

    'django.contrib.admin', 

    'django.contrib.auth', 

    'django.contrib.contenttypes', 

    'django.contrib.sessions', 

    'django.contrib.messages', 

    'django.contrib.staticfiles', 

    'myapp', 

]

Create a file called 



urls.py

 within the myapp directory and updated it with the following view.

from django.conf.urls import url 

from myapp import views 

 

urlpatterns = [ 



    url(r'^$', views.index, name='index'), 

    ]


Update the other 

urls.py


 file with the following content.

from django.conf.urls import url 

from django.contrib import admin 

from django.conf.urls import include 

from myapp import views 

 

urlpatterns = [ 



    url(r'^$', views.index, name='index'), 

    url(r'^myapp/', include('myapp.urls')), 

    url(r'^admin/', admin.site.urls), 

]

Create a folder named 



templates

 within the myapp directory. Then create a file named 

index.html

 

inside of the templates directory. Fill it with the following content.



https://riptutorial.com/

37


 

 

 

    myapp 



 

 

   


Download 0.85 Mb.

Do'stlaringiz bilan baham:
1   2   3   4   5




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling