Я впервые использую Django, и я пытаюсь создать модели для моего блога DIY, и я столкнулся с проблемой.
Я изменил свою модель Post и views.py, чтобы использовать id вместо используя слагов для запроса моих сообщений.
Я установил имя переменной id на id
. когда Django не позволил мне сделать это, я изменил его на индекс. Итак, что я сделал не так?
Ошибка
Apply all migrations: admin, auth, blog, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying blog.0001_initial... OK
Applying blog.0002_post_category... OK
Applying blog.0003_auto_20200404_2320...Traceback (most recent call last):
File "/home/brad/.local/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql)
File "/home/brad/.local/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py", line 394, in execute
return Database.Cursor.execute(self, query)
sqlite3.OperationalError: duplicate column name: id
models.py
from django.db import models
from django.utils import timezone
from django.urls import reverse
# Create your models here.
# Post model to store a blog post's data
class Post(models.Model):
title = models.CharField(max_length=200, unique=True, primary_key=True)
Index = models.IntegerField(default=0, auto_created=True)
body = models.TextField()
posted = models.DateField(db_index=True, auto_now_add=True)
specialty = models.CharField(max_length=200, default='TBA', unique=True)
def get_absolute_url(self):
return reverse('post', args=Index)
# publish blog
def publish(self):
self.published_date = timezone.now()
self.save()
# Category for the blogs
class Category(models.Model):
title = models.CharField(max_length=100, unique=True)
postings = models.ForeignKey(
Post, blank=True, null=True, on_delete=models.CASCADE)
slug = models.SlugField(max_length=100, db_index=True)
def post_query(self):
return Post.objects.filter(category=title)
def get_absolute_url(self):
return reverse('post-category', args=title)
def __unicode__(self):
return '%s' % self.title
def __str__(self):
return self.title
views.py
from django.http import HttpResponse
from django.shortcuts import render
from .models import Category, Post
def index(request):
context = {
'categories_list': Category.objects.all(),
'category.url': Category.get_absolute_url,
'category.title': Category.title
}
return render(request, 'blog/templates/index.html', context=context)
def category(request, primary_key):
context = {
'category.title': Category.objects.get(slug=primary_key),
'post_list': Category.post_query(),
'post.url': Post.get_absolute_url,
'post.title': Post.title,
'post.body': Post.body,
'post.date': Post.posted,
}
return render(request, 'blog/templates/category.html', context=context)
def post(request, primary_key):
context = {
'post.title': Post.title,
'post.date': Post.body,
'post.url': Post.get_absolute_url,
'post.body': Post.body
}
return render(request, 'blog/templates/post.html', context=context)