Home ยป How To Django?

How To Django?

How do you create a new project?

Answer: django-admin startproject name

How do you run your server?

Answer: python3 manage.py runserver

What local host will the project display on?

Answer: 8000

How do you create a new app in django?

Answer: python manage.py startapp name

Every app has three very important files. Those are:

Answer: urls.py, models.py, and views.py

How do you add an app to your project?

Answer: You visit the settings.py file and add it to the installed apps array. update url patterns in urls.py to include your apps urls.

How you include an apps urls?

Answer: url(r’^name/’, include(‘name.urls’))

Where does the include function come from?

Answer: django.conf.urls

How do you create view in the views.py file?

Answer:

from django.shortcuts import render
from django.http import HttpResponse

def nameofview(request):
return..

How do you create a custom urls.py file for your app and return one of your views?

Answer:

from django.conf.urls import url
from . import views

urlpatterns = [
url(r’^$’, views.index, name=”index”)]

What is Jinja?

Answer: templating language for django.

How do you return an html file from your template?

Answer: from django.shortcuts import render

def index(request):
return render(request, ‘location/file.html’, optionaldictionary)

Where do you keep templates for you app?

Answer: you make a templates directory

How do we put a marker to inject html inside a template?

Answer:

{% block content %}
{% endblock %}

How do we expand an html template?

Answer:

{% extends “location/header.html” %}

{% block content %}
content
{% endblock %}

What is another way of using html inside an html template?

Answer:

{% include “location/includes/snippet.html” %}

Where do you add things like bootstrap?

Answer: In a static directory. you also want to add in the settings.py of your app.
STATIC_URL = ‘/static/’

How do you include static file in your header template?

Answer:

{% load staticfiles %}
<link rel=”stylesheet” href=”{% static ‘css/bootstrap.min.css’ %}” type=’text/css’>

How do you pass variables from python to html with jinja?

Answer: You go to the views.py file and add a third parameter to the render method. A dictionary.

How do you loop in jinja?

Answer:

{% for c in content %}
# content comes from the dictionary passed down
{% endfor %}

You models.py file basically represents what?

Answer: Your database.

How can you create a model?

Answer:

class Post(models.Model):
title = modles.Charfield(max_length=140)

def __str__(self):
return self.title

When you make models you should think?

Answer: make migrations

What are the actions we need to make in order to use models?

Answer: Create and apply migrations

How do we make migrations?

Answer: python3 manage.py makemigrations app.

How do we run migrations?

Answer: python3 manage.py migrate

How do you create a super user in django?

Answer: python3 manage.py createsuperuser.

Where do you register your models to work with admin?

Answer: admin.py

How do import your models?

Answer:

from django.contrib import admin
from app.models import modelname

admin.site.register(modelname)