models.py
from django.db import models
class NewsUnit(models.Model):
catego = models.CharField(max_length=10,null=False)
nickname = models.CharField(max_length=20,null=False)
title = models.CharField(max_length=50,null=False)
message = models.TextField(null=False)
pubtime = models.DateTimeField(auto_now=True)
enabled = models.BooleanField(default=False)
press = models.IntegerField(default=0)
def __str__(self):
return self.title
views.py
from django.shortcuts import render
from newsapp import models
import math
# Create your views here.
page1 = 1
def index(request,pageindex=None):
global page1
pagesize = 8
newsall = models.NewsUnit.objects.all().order_by('-id')
datasize = len(newsall)
totpage = math.ceil(datasize/pagesize)
if pageindex == None:
page1 = 1
newsunits = models.NewsUnit.objects.filter(enabled=True).order_by('-id')[:pagesize]
elif pageindex == '1':
start = (page1-2)*pagesize
if start >= 0:
newsunits = models.NewsUnit.objects.filter(enabled=True).order_by('-id')[start:(start+pagesize)]
page1 -= 1
elif pageindex == '2':
start = page1*pagesize
if start < datasize:
newsunits = models.NewsUnit.objects.filter(enabled=True).order_by('-id')[start:(start+pagesize)]
page1 += 1
elif pageindex == '3':
start = (page1-1)*pagesize
if start < datasize:
newsunits = models.NewsUnit.objects.filter(enabled=True).order_by('-id')[start:(start+pagesize)]
currentpage = page1
return render(request,'index.html',locals())
def detail(request, detailid=None):
unit = models.NewsUnit.objects.get(id=detailid)
category = unit.catego
title = unit.title
pubtime = unit.pubtime
nickname = unit.nickname
message = unit.message
unit.press += 1
unit.save()
return render(request, "detail.html", locals())
urls.py
from django.contrib import admin
from django.urls import path
from newsapp import views
urlpatterns = [
path('',views.index),
path('index/<pageindex>/',views.index),
path('detail/<detailid>/',views.detail),
path('admin/', admin.site.urls),
]
For this django project, I would like to insert the new record on the database list after adding a record from the admin panel. It works if I clicked save record in the admin panel but however the current new record cannot be added into the database on http://127.0.0.1:8000/index/1/
. May I ask if I would like to have the record being inserted anything I am missing?
Aucun commentaire:
Enregistrer un commentaire