#models.py
from django.db import models
#Create your models here.
class Topic(models.Model):
text = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.text
class Entry(models.Model):
topic = models.ForeignKey(Topic,on_delete=models.CASCADE)
text = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = 'entries'
def __str__(self):
return f'{self.text[:50]}...'
#views.py
from django.shortcuts import render
#Create your views here.
def index(request):
return render(request,'/Users/fym/study_logs/index.html')
#views.py
from django.urls import path
from . import views
app_name = 'study_logs'
urlpatterns = [
path('',views.index,name='index'),
]
需要在settings.py文件中设置TEMPLATES下的DIRS,如下:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
'/home/html/example.com',
'/home/html/default',
],
},
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'DIRS': [
'/home/html/jinja2',
],
},
]
其中 DIRS就是html模板文件的路径