When I add an app with the name advertisements (add models, admin + register in INSTALLED_APPS) it is not listed in the admin interface (it is very hard to see but it disappears after microseconds).
models.py:
from django.db import models
# Create your models here.
class Advertisement(models.Model):
pass
admin.py:
from django.contrib import admin
from .models import Advertisement
# Register your models here.
#admin.register(Advertisement)
class AdvertisementAdmin(admin.ModelAdmin):
pass
apps.py:
from django.apps import AppConfig
class AdvertisementsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'advertisements'
settings.py:
INSTALLED_APPS = [
# ...
"advertisements",
]
Changing an app directory to something like advertisements2, app name (in apps.py) to advertisements2 solves the problem.
Why the app with the name 'advertisements' is not listed in the admin interface?" What is the issue with the name "advertisements"?
I tested in many times in completely blank project.
The question seemd to be a bit strange for me at the beginning and I was feeling super curious about what is going on. I recreated your code perfectly and I was getting the same result as you. No advertisemets app in admin panel. I started a debugger and tracked execution line by line, and everything seemd to be in order. But after some time it occured to me, that it could not be actually django's fault. And it is not! Beware the most intriguing solution to your problem:
Just disable your adblocker and refresh the site!
What is most problably the problem (and what was the problem on my site) is that my adblocker blocked the word advertisement and all the styles that came with it. So with your adblocker turned on, if you were to inspect a site you would see your advertisements app sitting comfortably in your html code:
<div class="app-advertisements module">
<table>
<caption>
Advertisements
</caption>
<tbody><tr class="model-advertisement">
<th scope="row">Advertisements</th>
If you were to remove app-advertisements class from <div class="app-advertisements module"> (again, with the adblocker still turned on) the Advertisement should magically apear in admin panel.
Try using admin.site.register(Advertisement) instead of the AdminView in admin.py. If this line works, you could add the model admin view individually like this:
class AdvertisementAdmin(admin.ModelAdmin):
pass
admin.site.register(Advertisement, AdvertisementAdmin)
I am creating a blog using Django.I want to fetch my post from the database using Queryset by using command Post.objects.filter(published_date__lte=timezone.now())
But I am getting blank output. I have already imported timezone using
from django.utils import timezone
Here's a screenshot:
Post.objects.filter(published_date__lte=timezone.now())
This filter will not return any posts where published_date is None.
The tutorial gives instructions how to publish a post.
>>> post = Post.objects.get(title="Sample title")
>>> post.publish()
This will set published_date for that post, so now that post should be returned if you re-run the filter command.
>>> Post.objects.filter(published_date__lte=timezone.now())
It is showing empty because your command is asking for the published post and yet no post is published.
Try this:
post = Post.objects.get(title="Sample title") #Sample title is the name of the title
post.publish()
Post.objects.filter(published_date__lte=timezone.now())
<QuerySet [<Post: Sample title>]>
Hope it helps.
Hello everyone how is it going?
I've just started using Django recently, and I've started getting my head around it; I need to build a website about cars, with two major APPS:
CarsCatalogue;
News Section;
I find the fact that I can manage the news from the admin panel extremely useful. I have created the typical model:
class Post(models.Model):
title = models.CharField(max_length = 140)
date = models.DateTimeField()
body = models.TextField()
def __str__(self):
return self.title
With the urls.py as follows
url(r'^(?P<pk>\d+)$', DetailView.as_view(
model = Post,
template_name="news/post.html"))
url(r'^$', ListView.as_view(
queryset=Post.objects.all().order_by("-date")[:25],
template_name="news/news.html")),
This is great! I can manage the News app Extremely easyly from the admin panel. Now I have a page for each news: news/1; news/2 etc etc;
But when i go down to the CarsCatalogue, and I would really need to simplify my life because I have plenty of cars with a personal page each to add, I am instead finding myself needing to modify the urls.py for each car I need to add, and it seems I have to modify the views.py for each car -I am using render- am I right?
I mean, does it make sense to have a Views.py with one hundred different functions calling one hundred pages?
And then if I want to create a list with all the urls of the CarCatalogue, having to write every link one by one?
Is this the way to use Django in this case?
I would create another "news-style" APP for CarsCatalogue, that would be so much more easy for me to manage through the Admin Panel, but I need each url to show the car name, like: CarsCatalogue/Seat-Ibiza and not like CarsCatalogue/1.
Maybe I can do something like the news APP, but changing the way urls are generated and shown?
I am asking you all of this after I read the documentation and several Google topics and other resources;
I hope you guys will be able to clear the fog around my head;
With all the Respect such a community deserves,
Sincerely,
-oKi
EDIT n*1
It's been 3 hours of reading, trying, modifying, erasing, trying again.
I read a lot of stuff, but at the same time I got pheraps even more confused, because I found so many things while looking for how to "slug"ify the urls (that is indeed what I was looking for [now I can indeed use the admin panel to do what I wanted!] thanks) that I ended up mixing a lot of stuff. So, using the NEWS application, what I have done so far:
python3.5 manage.py flush, makemigrations, migrate, createsuperuser
I modified the news/models.py, so that it now looks like this:
from django.db import models
from django.template.defaultfilters import slugify
class Post(models.Model):
title = models.CharField(max_length = 140)
date = models.DateTimeField()
body = models.TextField()
slug = models.SlugField(title, max_length=100, unique=True)
def __str__(self):
return self.title
def slug(self):
return slugify(self.title)
I modified the news/admin.py, so that it now looks like this:
from django.contrib import admin
from news.models import Post
admin.site.register(Post)
class NewsAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug": (Post.slug)} <!-- that seems makes sense looking at the Model - I also tryed {"slug": (title,)}, {"slug": (Post.title)}-->
I modified the news/urls.py, so that it now looks like this:
from django.conf.urls import url
from django.views.generic import ListView, DetailView
from news.models import Post
urlpatterns = [
url(r'^$', ListView.as_view(
queryset=Post.objects.all().order_by("-date")[:25],
template_name="news/news.html")),
url(r'^(?P<slug>[\w-]+)$', DetailView.as_view(
model = Post,
template_name="news/post.html")),
]
then I give:
python3.5 manage.py runserver
and it runs with no problem.
So I go to /admin, I create a new post, it creates it...
But then what happens?
It still uses the old "paradigm" to generate the url, including the old keys(id's) it was using before I erased the client... so... what I expected to be something like mysite/news/new-human-level-urld-news
turns out to be mysite/news/11.
LOL :D and, luckily, the browser gives me also an error...
"FieldError at /news/11
Cannot resolve keyword 'slug' into field. Choices are: body, date, id, title"
at the moment I can't find an answer, I find so much stuff that I just don't know how to mix things up. I will keep searching. Help is appreciated! Thanks for now!!! :D
According to my understanding to your problem, what you need is not to create a view function for every car you have.
Indeed, you need to create 1 template (html page) that describes your CarsCatalogue, where all the CarsCatalogues have the same structure with different information. Then the user chooses one CarsCatalogue, let's say from a dropdown control and then you load the data from your database and show your results in the template.
In order to achieve this, have a look on the following topics:
Django forms. And in order to get your data from the database, you need the the id for example to be passed in the url, you can see this answer how to pass an id in django url or from the official website URL Dispatcher.
In few words, django is a simple way to do your website and definitely no need to rewrite your code several times.
NO THERE IS NO NEED TO CREATE MULTIPLE URLS.
From what I understood, you want a catalogue for each car.
Just create a view from where you display the names of all your cars.
Then when you click on a car, make a get request to another view function in the get request pass your selected car details. Now comes the use on Django Templates. Create a Django template for your catalogue, pass the car specific data to it.
You need to read about Django Templates , I think Django templates is something that will solve your problem.
I think the thing you're missing is that the parameter in the URL can be anything, not just a numeric URL. If it's a string, we call that a slug, and the Django admin will automatically create a slug from the fields in your model if you set the prepopulated_fields option.
Then, you can use that slug in the URL:
url(r'^(?P<slug>[\w-]+)$', DetailView.as_view(
model = Car,
template_name="news/car.html"))
and Django will use that field instead of the ID to find the right content to display.
I found a way to fix my problem.
Even though I started off by asking about the CARS Catalogue, I will explain what I did using the NEWS APP instead, as the logic is the same and as that is the APP I have modified in the first EDIT of my post.
Inside the Templates, have a "news.html" page to render the list of all the Post, and a "post.html" to render every single Post.
The following is part of the procedure on how to set an APP that you can manage directly from the ADMIN PANEL, and that uses the SLUG field as URL.
models.py:
from django.db import models
class Post(models.Model):
title = models.CharField(max_length = 140)
body = models.TextField()
slug = models.SlugField(unique=True)
def __str__(self):
return self.title
admin.py:
from django.contrib import admin
from news.models import Post
class PostAdmin(admin.ModelAdmin):
model = Post
prepopulated_fields = {'slug': ('title',)}
admin.site.register(Post, PostAdmin)
urls.py:
from django.conf.urls import url
from django.views.generic import ListView, DetailView
from news.models import Post
urlpatterns = [
url(r'^$', ListView.as_view(
queryset=Post.objects.all(),
template_name="news/news.html")),
url(r'^(?P<slug>[\w-]+)$', DetailView.as_view(
queryset=Post.objects.all(),
model = Post,
template_name="news/post.html")),
]
I hope that can be useful to somebody.
Jeez you gotta study hard to use this Framework :D
Thanks everybody for the support, c ya :)
Today I decided to use an django-geoposition app to show some maps in my project. The problem is that while reading the Docs on github I still don't know what should I write in the admin.py so I could add some maps. Can someone help?
Docs: GitHub of django-geoposition
According to the Documentation, you just have to add a GeopositionField in an attribute:
from django.db import models
from geoposition.fields import GeopositionField
class YourModel(models.Model):
name = models.CharField(max_length=100)
map_field = GeopositionField()
So when you go to the model-page on the admin, you can see a map that you can search a location and get the latitude and longitude.
And if you want to allow choosing a location by clicking on the map, just add a few lines on the settings.py:
GEOPOSITION_MARKER_OPTIONS = {
'cursor': 'move'
}
https://github.com/philippbosch/django-geoposition
That's all.
I'm hosting a Django site through mod_wsgi and WAMP with Python 2.7. On my admin site the Users, Groups and Sites sections all have Add and Change buttons. While there is a section each for both of my own custom models, Feedpost and Newspost, they have no buttons at all and there is no way to add or change them.
I believe this change came about when I switched from using the Django internal testing server to using WAMP. Does anyone know what's causing this and how to get my Add and Change buttons back?
EDIT:
Here's one of the two models:
from django.db import models
from django.contrib import admin
class FeedPost(models.Model):
title = models.CharField(max_length=50)
text = models.CharField(max_length=5000)
date = models.DateTimeField('Date Published')
def __unicode__(self):
return self.title
admin.site.register(FeedPost)
The other is nearly exactly the same.
It turns out that using the Django development server localhost:8000/admin insists on going to my site's homepage and I can't access the admin site at all.
Also I think this is going to turn out to be the problem because I haven't got an admin.py file anywhere in my project...
These need to be in the admin.py file for starters.
from django.contrib import admin
admin.site.register(FeedPost)