I have a model called Post for my Blog application in Django. I have a slug field that is prepopulated by the title field. (I also use the get_absolute_url method, if that matters). What I want is to be able to see the permalink, of course without being able to edit it! Just readonly. I also believe that this field will be empty when I create a post, so only visible-active when I update a post.
Thanks in advance.
Related
I'm making a web app which allows users to register to the site and let them post their posts.
For taking in their post I have used the built in CBV: CreateView. In the user post I take in the name of the user using foreignkey with null=True, image, title.
The problem is: when I go to the detail page of that post it shows that there is no file associated with the image field, so I made an if statement to check if the post contains an image. But then too, it does not show the image. It shows User:None.
I don't know what is happening, I have read that CBV take the self.request automatically and there is no need to link the current user to the post.
Following are the screenshots of my code:
models.py
browser image before clicking submit button
browser image after clicking submit button (detail page of the post)
post_detail.html
views.py
If the image is in you database than you can render it this way
<img src="{{post_detail.image.url}}">
I found the answer.
I forgot to add the enctype="multipart/form-data" in the form of my createview html.
Now it's working perfectly fine.
Screenshot of userpost_form.html
Thank you all for all the support.
I have a basic Django blogging application with the fields such as textfield, datetime, imagefield, slug etc. I want to have a feature where I can publish and unpublish posts in the admin interface(with the help of checkbox). How can this be done?
Add a BooleanField that describes whether or not the post is published, and check the value of this field in your views.
In Django, I made a UserProfile class and linked it to the User with
user = models.OneToOneField(User)
That works fine, looks like this:
Where the UserProfile is visible (with only one field).
However, I am also using the list_editable option that I discovered today. I can get the User fields editable in list view, like this:
Very nice feature, but one I can find little information about.
What I want to do is add UserProfile fields to this list in an editable way (the email_verified field in the above example, to begin with).
Is that possible to do (without changing Django code)? If so, how would I do that?
Many thanks,
UPDATE: Seriously, how can I make the question more useful if I can't use links, images, nothing? Information about list_editable is in the Django help, hopefully Google helps.
You can use inheritance of User model and get rid from Profile. In my opinion this is better way. Here you can read about this: http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/
And then just add email_verified to list_editable
I have a Customer model which contains a ForeignKey to a Contact model.
I have over 100,000 contacts in my DB and when I load the admin page for a specific customer, the dropdown menu for the contact is getting populated with ALL of the contacts in the database. This has recently, due to its shear length, started causing my Firefox to crash while the admin page is loading.
Is there a way to either:
replace the field with an integer
field I can manually modify to the
contact ID when necessary
replace the dropdown menu with some
alternative input method which won't
crash the browser
remove this input
from the Customer admin page
altogether
Thanks!
You can do any of the either of things you want to.
Simplest solution is the exclude the field from the admin. Just say so in the admin class.
You can change the field to be text input and display it's primary key rather than the item itself, by including it in the raw_id_fields of the admin class.
You can also replace the standard dropdown widget with the Auto complete text field input. Use the implemented widget, or other equivalents. - This is probably the solution you like the best.
You can also override the formfield_for_foreignkey method on the Admin model to customize the queryset that gets displayed in the foreign-key dropdown. You may want to checkout my implementation for displaying only the current User's (or subdomain's) added entities.
Sounds like specifying the contact field in raw_id_fields in your admin.py entry for the relevant model would sort you out. Docs are here.
PS. Surprised (but not that surprised) that FF gives out before your database server tanks...
in the django admin, in any app model, the diplay data (datagrid o view template, i dont know how call it), django show "all" the entry, but what happen if i want that django just show the entry with statu live or x status and not all entry with any status?.
how, overriding initial behavior in admin model?
Thanks
Here's the same question with some answers.