basic Flashcards
1
Q
urls example
A
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path(‘polls/’, include(‘polls.urls’)),
path(‘admin/’, admin.site.urls),
]
2
Q
model example
A
from django.db import models
class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField('date published')
class Choice(models.Model): question = models.ForeignKey(Question, on_delete=models.CASCADE) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0)
3
Q
get sql of migration
A
python manage.py sqlmigrate polls 0001
4
Q
import HttpResponse
A
from django.http import HttpResponse
5
Q
template loader example
A
from django.http import HttpResponse
from django.template import loader
from .models import Question
def index(request): latest_question_list = Question.objects.order_by('-pub_date')[:5] template = loader.get_template('polls/index.html') context = { 'latest_question_list': latest_question_list, } return HttpResponse(template.render(context, request))
6
Q
render example
A
from django.shortcuts import render
from .models import Question
def index(request): latest_question_list = Question.objects.order_by('-pub_date')[:5] context = {'latest_question_list': latest_question_list} return render(request, 'polls/index.html', context)
7
Q
Http404
A
from django.http import Http404
from django.shortcuts import render
from .models import Question # ... def detail(request, question_id): try: question = Question.objects.get(pk=question_id) except Question.DoesNotExist: raise Http404("Question does not exist") return render(request, 'polls/detail.html', {'question': question})
8
Q
register model for admin
A
from django.contrib import admin
from .models import Choice, Question # ... admin.site.register(Choice)