I'm new to web development, and i'm building a blog web app in Django.
I'm having problems adding new posts through django-admin. Clicking the "add" button gives me an "Error during template rendering - 'str' object has no attribute 'utcoffset'" error message.
models.py
from django.db import models
from django.utils import timezone
import datetime
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=50)
content = models.TextField(blank=True, null=True)
post_date = models.DateTimeField(default='date posted')
def published_recently(self):
now = timezone.now()
return now - datetime.timedelta(days=1) <= self.post_date <= now
published_recently.admin_order_field = 'post_date'
published_recently.boolean = True
published_recently.short_description = 'Recent post?'
def __str__(self):
return self.title
class Customer(models.Model):
username = models.CharField(max_length=50)
firstname = models.CharField(max_length=50)
lastname = models.CharField(max_length=50)
email = models.CharField(max_length=50)
country = models.CharField(max_length=50)
reg_date = models.DateTimeField(default='date reg')
def __str__(self):
return self.username
class Comment(models.Model):
comment_cont = models.TextField(max_length=200)
user_name = models.ForeignKey(Customer, default=0, on_delete=models.CASCADE)
comment_post = models.ForeignKey(Post, on_delete=models.CASCADE)
votes = models.BigIntegerField(default=0)
comment_date = models.DateTimeField(default='date commented')
def __str__(self):
return self.comment_cont
class Feedback(models.Model):
feedback_title = models.CharField(max_length=50)
feedback_detail = models.CharField(max_length=200)
user_name = models.ForeignKey(Customer, default=1, verbose_name='user_name', on_delete=models.SET_DEFAULT)
admin.py
from django.contrib import admin
from .models import Post, Customer, Feedback, Comment
# Register your models here.
class CommentInLine(admin.TabularInline):
model = Comment
extra = 1
fields = ['comment_cont', 'votes', 'comment_date']
class CustomerAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['firstname', 'lastname', 'username', 'email', 'country', 'reg_date']})
]
class PostAdmin(admin.ModelAdmin):
fieldsets = [
('Post', {'fields': ['title', 'content']}),
('Date information', {'fields': ['post_date'], 'classes':['collapse']}),
]
inlines = [CommentInLine]
list_display = ('title', 'content', 'post_date', 'published_recently')
list_filter = ['post_date']
search_fields = ['title']
class FeedbackAdmin(admin.ModelAdmin):
fieldsets = [
('Feedback', {'fields': ['feedback_title', 'feedback_detail', 'user_name']}),
]
list_display = ('user_name', 'feedback_title', 'feedback_detail')
# inlines = [CustomerInLine]
# list_display = ('user_name', 'feedback_title', 'feedback_detail')
class CommentAdmin(admin.ModelAdmin):
fields = ['user_name', 'comment_post', 'comment_cont', 'votes', 'comment_date']
admin.site.register(Post, PostAdmin)
admin.site.register(Feedback, FeedbackAdmin)
admin.site.register(Customer, CustomerAdmin)
admin.site.register(Comment, CommentAdmin)
I want to be able to add posts through Django admin. Any other corrections to the code is highly welcome. I appreciate the help. Thanks.
post_date = models.DateTimeField(default='date posted')
the post_date is DateTimeField and you have provided a string as default . It should be an instance of datetime.datetime.
post_date = models.DateTimeField(default=timezone.now)
# or
post_date = models.DateTimeField(auto_now_add=True, verbose_name='Publish Time')
should fix the problem
Related
I'm building a blog aplication with DRF, and I want it to be able to get my get_absolute_url method for each post, like: http://127.0.0.1:8001/blog/posts/2022/10/11/my-post
And not the default: http://127.0.0.1:8001/blog/posts/1
Here's my model:
class Post(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'), ('published', 'Published')
)
title = models.CharField(max_length=250)
slug = models.SlugField(unique_for_date='publish', max_length=250)
body = models.TextField()
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='blog_posts')
publish = models.DateTimeField(default=timezone.now)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
category = models.ForeignKey(Category, on_delete=models.SET_NULL, related_name="posts" ,blank=True, null=True )
objects = models.Manager()
published = PublishedManager()
tags = TaggableManager()
feature_image = models.ImageField(upload_to="uploads/", null=True, blank=True)
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', args=[self.publish.year,
self.publish.month,
self.publish.day,
self.slug], )
And my serializer:
class BlogSerializer(serializers.HyperlinkedModelSerializer):
author = serializers.ReadOnlyField(source='author.username')
url = serializers.CharField(source='get_absolute_url', read_only=True)
class Meta:
model = Post
fields = ['url', 'id', 'title', 'slug', 'body', 'author', 'publish', 'created_at', 'updated_at',
'status', 'category', 'feature_image']
Somebody help how to do that, please.
thanks!
Edit:
This is the error that I get when running the app:
NoReverseMatch at /blog/posts/
Reverse for 'post-detail' with arguments '(2022, 10, 11, 'first-post-2')' not found. 2 pattern(s) tried: ['blog/posts/(?P[^/.]+)\.(?P[a-z0-9]+)/?$', 'blog/posts/(?P[^/.]+)/$']
From what you've posted and what I've seen you don't seem to be doing anything wrong.
Could you describe what that spits out?- is it crashing?- is the url wrong?- does it need the website prefix?
I've also seen this, /, which might be a route you could take. (I've done this with custom admin pages)
class BlogSerializer(serializers.HyperlinkedModelSerializer):
url = serializers.SerializerMethodField()
def get_url(self, obj):
return obj.get_absolute_url()
class Meta:
model = Post
fields = ['url', 'id', 'title', 'slug', 'body', 'author', 'publish', 'created_at', 'updated_at',
'status', 'category', 'feature_image']
Edit
Yeah, that looks like a problem with your Urls. the Docs show this:
from django.urls import path
from . import views
urlpatterns = [
path('articles/2003/', views.special_case_2003),
path('articles/<int:year>/', views.year_archive),
path('articles/<int:year>/<int:month>/', views.month_archive),
path('articles/<int:year>/<int:month>/<slug:slug>/', views.article_detail),
]
So your path might end up like:
Note: Idk what that last one is supposed to be, I just put title.. you use slug for any string, if you end up doing a pk instead use int
urlpatterns = [
path('posts/<int:year>/<int:month>/<int:day>/<slug:title>/', views.post_detail),
]
For Post model I created a custom manager:
#models.py
class ObjectsOnManager(models.Manager):
def get_queryset(self):
return super(ObjectsOnManager, self).get_queryset().filter(status='on')
class OnOffStatusModel(models.Model):
ON = 'on'
OFF = 'off'
STATUS_CHOICES = (
(ON, 'Показывать'),
(OFF, 'Не показывать'),
)
status = models.CharField("Статус", max_length=15, choices=STATUS_CHOICES, default=ON)
objects_on = ObjectsOnManager()
objects = models.Manager()
class Meta:
abstract = True
class Post(OnOffStatusModel):
count = models.PositiveIntegerField("Показы", default=0)
title = models.CharField("Заголовок", max_length=50)
descript = models.CharField("Описание", max_length=100)
slug = models.SlugField(max_length=50, unique=True)
category = models.ManyToManyField(Category, related_name='posts')
pub_date = models.DateTimeField("Дата публикации", default=timezone.now, blank=True)
body = models.TextField("Текст поста", max_length=2500)
main_post = models.BooleanField("Главная новость", default=False)
Then changed get_queryset in modelAdmin
#admin.py
class PostAdmin(admin.ModelAdmin):
form = PostImageControlForm
fields = ('count', 'status', 'image', 'title', 'descript', 'body', 'main_post', )
list_display = ('title', 'main_post', 'count',)
list_editable = ('main_post', 'status')
list_filter = ('category', 'main_post', 'status')
def get_queryset(self, request):
return Post.objects.all()
So if I edit model on the change model page it is ok, but if i tried to edit on the change list page I have got the error
enter image description here
What I want: I have the model classes named Book and Issue. In Book, there is a field named quantity that defines how many books are there. When the Admin issue a book to a student, there will be a -1 in quantity. how may I implement this. N.B: I want to implement this in Django Admin Dashboard
from django.db import models
from datetime import date, timedelta, datetime
# Create your models here.
class User(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
depertment_name = models.CharField(max_length=30)
roll_number = models.CharField(max_length=10, primary_key=True)
semester = models.CharField(max_length=10)
def __str__(self):
return self.depertment_name.upper() + ', ' + self.roll_number + ', ' + self.first_name.upper()
class Book(models.Model):
name = models.CharField(max_length=30)
author_name = models.CharField(max_length=30)
category = models.CharField(max_length=30)
quantity = models.IntegerField()
def __str__(self):
return self.name
class Issue(models.Model):
issue_id = models.CharField(max_length=10)
roll = models.ForeignKey(User, on_delete=models.CASCADE)
book_name = models.ForeignKey(Book, on_delete=models.CASCADE)
issue_date = models.DateField(auto_now=False, auto_now_add=True)
def return_date(self):
return datetime.now()+timedelta(days=3)
def fine(self):
return_date = datetime.now() + timedelta(days=3)
if datetime.now() > return_date:
return 'Fine'
else:
return 'No Fine'
UPDATE: IMPLEMENTATION IS SOLVED. NEED TO SHOW RESULT
please take a look here
ADMIN.PY CODE
from django.contrib import admin
from django.contrib.auth.models import Group
from .models import User, Book, Issue
admin.site.site_header = 'Library Management System'
class UserAdmin(admin.ModelAdmin):
list_display = ('roll_number', 'first_name', 'last_name', 'depertment_name')
list_filter = ('depertment_name', 'semester')
search_fields = ('roll_number', 'first_name')
class BookAdmin(admin.ModelAdmin):
list_display = ('name', 'author_name', 'remaining', 'category')
list_filter = ('category', 'author_name')
search_fields = ('name', 'author_name')
class IssueAdmin(admin.ModelAdmin):
list_display = ('issue_id', 'issue_date', 'return_date', 'fine', 'save')
list_filter = ('issue_date',)
search_fields = ('issue_id',)
admin.site.register(User, UserAdmin)
admin.site.register(Book, BookAdmin)
admin.site.register(Issue, IssueAdmin)
admin.site.unregister(Group,)
I think, overriding save() method of Issue model would solve the problem,
class Issue(models.Model):
# your code
def save(self, *args, **kwargs):
if not self.pk:
super().save(*args, **kwargs)
self.book_name.quantity -= 1
self.book_name.save()
else:
super().save(*args, **kwargs)
Update-1
Inorder to get see the quantity of books, use book_name__quantity in list_display of IssueAdmin.
class IssueAdmin(admin.ModelAdmin):
list_display = ('issue_id', 'issue_date', 'return_date', 'fine', 'book_name__quantity')
list_filter = ('issue_date',)
search_fields = ('issue_id',)
I imported a project from Django builder, managed to install the dependencies, and start the admin, but I have an issue.
All of the fields in admin are not editable! None of them.
Here's a screenshot of the issue:
Here's the relevant files:
Admin.py
from django.contrib import admin
from django import forms
from .models import Tours, Locations, Photos, PlaceLocations, Information
class ToursAdminForm(forms.ModelForm):
class Meta:
model = Tours
fields = '__all__'
class ToursAdmin(admin.ModelAdmin):
form = ToursAdminForm
list_display = ['name', 'slug', 'created', 'last_updated', 'Tags', 'Photo', 'Description', 'Duration']
readonly_fields = ['name', 'slug', 'created', 'last_updated', 'Tags', 'Photo', 'Description', 'Duration']
admin.site.register(Tours, ToursAdmin)
class LocationsAdminForm(forms.ModelForm):
class Meta:
model = Locations
fields = '__all__'
class LocationsAdmin(admin.ModelAdmin):
form = LocationsAdminForm
list_display = ['name', 'slug', 'created', 'last_updated', 'Photo', 'MainLocLat', 'MainLocLon', 'MainLocRad', 'Audio', 'Description']
readonly_fields = ['name', 'slug', 'created', 'last_updated', 'Photo', 'MainLocLat', 'MainLocLon', 'MainLocRad', 'Audio', 'Description']
admin.site.register(Locations, LocationsAdmin)
class PhotosAdminForm(forms.ModelForm):
class Meta:
model = Photos
fields = '__all__'
class PhotosAdmin(admin.ModelAdmin):
form = PhotosAdminForm
list_display = ['name', 'slug', 'created', 'last_updated', 'Photo']
readonly_fields = ['name', 'slug', 'created', 'last_updated', 'Photo']
admin.site.register(Photos, PhotosAdmin)
class PlaceLocationsAdminForm(forms.ModelForm):
class Meta:
model = PlaceLocations
fields = '__all__'
class PlaceLocationsAdmin(admin.ModelAdmin):
form = PlaceLocationsAdminForm
list_display = ['name', 'slug', 'created', 'last_updated', 'Latitude', 'Longitude', 'Radius']
readonly_fields = ['name', 'slug', 'created', 'last_updated', 'Latitude', 'Longitude', 'Radius']
admin.site.register(PlaceLocations, PlaceLocationsAdmin)
class InformationAdminForm(forms.ModelForm):
class Meta:
model = Information
fields = '__all__'
class InformationAdmin(admin.ModelAdmin):
form = InformationAdminForm
list_display = ['name', 'slug', 'created', 'last_updated', 'Photo', 'Description']
readonly_fields = ['name', 'slug', 'created', 'last_updated', 'Photo', 'Description']
admin.site.register(Information, InformationAdmin)
models.py
from django.core.urlresolvers import reverse
from django_extensions.db.fields import AutoSlugField
from django.db.models import *
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth import get_user_model
from django.contrib.auth import models as auth_models
from django.db import models as models
from django_extensions.db import fields as extension_fields
from smartfields import fields
from smartfields.dependencies import FileDependency
from smartfields.processors import ImageProcessor
class Tours(models.Model):
# Fields
name = models.CharField(max_length=255)
slug = extension_fields.AutoSlugField(populate_from='name', blank=True)
created = models.DateTimeField(auto_now_add=True, editable=False)
last_updated = models.DateTimeField(auto_now=True, editable=False)
Tags = models.TextField(max_length=100)
Photo = models.ImageField(upload_to="/upload/images/")
Description = models.TextField(max_length=2000)
Duration = models.IntegerField()
class Meta:
ordering = ('-created',)
def __unicode__(self):
return u'%s' % self.slug
def get_absolute_url(self):
return reverse('app_name_tours_detail', args=(self.slug,))
def get_update_url(self):
return reverse('app_name_tours_update', args=(self.slug,))
class Locations(models.Model):
# Fields
name = models.CharField(max_length=255)
slug = extension_fields.AutoSlugField(populate_from='name', blank=True)
created = models.DateTimeField(auto_now_add=True, editable=False)
last_updated = models.DateTimeField(auto_now=True, editable=False)
Photo = models.ImageField(upload_to="/upload/images/")
MainLocLat = models.TextField(max_length=100)
MainLocLon = models.TextField(max_length=100)
MainLocRad = models.TextField(max_length=100)
Audio = models.FileField(upload_to="/upload/files/")
Description = models.TextField(max_length=2000)
# Relationship Fields
Photos = models.ForeignKey('app_name.Photos', )
PlaceLocations = models.ForeignKey('app_name.PlaceLocations', )
class Meta:
ordering = ('-created',)
def __unicode__(self):
return u'%s' % self.slug
def get_absolute_url(self):
return reverse('app_name_locations_detail', args=(self.slug,))
def get_update_url(self):
return reverse('app_name_locations_update', args=(self.slug,))
class Photos(models.Model):
# Fields
name = models.CharField(max_length=255)
slug = extension_fields.AutoSlugField(populate_from='name', blank=True)
created = models.DateTimeField(auto_now_add=True, editable=False)
last_updated = models.DateTimeField(auto_now=True, editable=False)
Photo = models.ImageField(upload_to="/upload/images/")
class Meta:
ordering = ('-created',)
def __unicode__(self):
return u'%s' % self.slug
def get_absolute_url(self):
return reverse('app_name_photos_detail', args=(self.slug,))
def get_update_url(self):
return reverse('app_name_photos_update', args=(self.slug,))
class PlaceLocations(models.Model):
# Fields
name = models.CharField(max_length=255)
slug = extension_fields.AutoSlugField(populate_from='name', blank=True)
created = models.DateTimeField(auto_now_add=True, editable=False)
last_updated = models.DateTimeField(auto_now=True, editable=False)
Latitude = models.TextField(max_length=100)
Longitude = models.TextField(max_length=100)
Radius = models.IntegerField()
class Meta:
ordering = ('-created',)
def __unicode__(self):
return u'%s' % self.slug
def get_absolute_url(self):
return reverse('app_name_placelocations_detail', args=(self.slug,))
def get_update_url(self):
return reverse('app_name_placelocations_update', args=(self.slug,))
class Information(models.Model):
# Fields
name = models.CharField(max_length=255)
slug = extension_fields.AutoSlugField(populate_from='name', blank=True)
created = models.DateTimeField(auto_now_add=True, editable=False)
last_updated = models.DateTimeField(auto_now=True, editable=False)
Photo = models.ImageField(upload_to="/upload/images/")
Description = models.TextField(max_length=2000)
class Meta:
ordering = ('-created',)
def __unicode__(self):
return u'%s' % self.slug
def get_absolute_url(self):
return reverse('app_name_information_detail', args=(self.slug,))
def get_update_url(self):
return reverse('app_name_information_update', args=(self.slug,))
You should better remove the readonly_fields attribute on each ModelAdmin class. Doing that, the fields, in the admin panel, will not be read-only!
I have these model and admin files. It works nice but when I select one activity while adding new Gym Object in the select I see "Activity object" instead of the Activity title.
How can I change these?
# Model
from django.db import models
class Activity(models.Model):
title = models.CharField(max_length=250)
description = models.CharField(max_length=250)
class Gym(models.Model):
name = models.CharField(max_length=250)
pub_date = models.DateTimeField('date published')
where = models.CharField(max_length=250)
description = models.TextField()
timetable = models.TextField()
activities = models.ManyToManyField(Activity, through='GymActivity')
class GymActivity(models.Model):
gym = models.ForeignKey(Gym)
activity = models.ForeignKey(Activity)
# Admin
from django.contrib import admin
from gym.models import Gym, Activity, GymActivity
class GymActivityInline(admin.TabularInline):
model = Gym.activities.through
extra = 6
class GymAdmin(admin.ModelAdmin):
list_display = ['name', 'pub_date']
fieldsets = [
(None, {'fields': ['name']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
(None, {'fields': ['where']}),
(None, {'fields': ['description']}),
(None, {'fields': ['timetable']}),
]
inlines = [GymActivityInline]
class ActivityAdmin(admin.ModelAdmin):
list_display = ['title']
admin.site.register(Gym, GymAdmin)
admin.site.register(Activity, ActivityAdmin)
You are missing __unicode__ method on the models
Do this:
class Activity(models.Model):
title = models.CharField(max_length=250)
description = models.CharField(max_length=250)
def __unicode__(self):
return u'%s' % self.title
If you are using Python 3.x, just replace __unicode__ with __str__