Django file upload FilerFileField - django

I'm trying to use django FilerFileField as explained in the doc http://django-filer.readthedocs.io/en/latest/usage.html. It renders well with a field and a hyperlink inviting to select the file like http://localhost:8000/fr/admin/filer/folder/?_pick=file. This opens the filer admin page where I can navigate.
After file selection, how can I go back to my form page with the appropriate file selected?
I never get back such example as explained in the doc
Here is what I have in my main url pattern
urlpatterns = [
url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap',
{'sitemaps': {'cmspages': CMSSitemap}}),
url(r'^filer/', include('filer.urls')),
url(r'^filebrowser_filer/', include('ckeditor_filebrowser_filer.urls')),
]
I'm not using models.FileField() because I want to be able to point to my admin Filer images and files located in media/filer_public
The models.py is the same as described in the doc
from django.db import models
from filer.fields.image import FilerImageField
from filer.fields.file import FilerFileField
class Company(models.Model):
name = models.CharField(max_length=255)
logo = FilerImageField(null=True, blank=True,
related_name="logo_company")
disclaimer = FilerFileField(null=True, blank=True,
related_name="disclaimer_company")

Related

Django model database look up

I am generating video files triggered by POST request and saving them programmatically into the django model below.
How can I look up the uploaded file itself once uploaded to the database in a similar fashion to VideoUpload.objects(get)?
I don't have users only guests. I am using hash-id-field so far however can change this.
Models.py
from django.db import models
from django.conf import settings
import hashlib
from hashid_field import HashidAutoField
class VideoUpload(models.Model):
hashed_video_file_name = HashidAutoField(primary_key=True)
name = models.CharField(max_length=40)
videofile= models.FileField(upload_to='videos/', null=True)
objects = models.Manager()

Django admin not showing models - version 2.2

I am using Django 2.2. I don't know what I'm missing.
models.py
from django.db import models
class Efesto(models.Model):
nombre = models.CharField(max_length=150)
tipo = models.ForeignKey(Color, blank=True, null=True, on_delete=models.CASCADE)
....
def __str__(self):
return self.nombre
admin.py
from django.contrib import admin
from estrategia import models
# Register your models here.
admin.register(models.Efesto)
Do I need anything else?
When I open the admin, I can't see the Efesto model there. The admin.py file is created automatically by the startapp command. The urls include
...
path('admin/', admin.site.urls),
It has being a while since I code django, and this used to be enough to get the models registered. The app is included in settings.INSTALLED_APPS correctly. Any advice will help.
You have to use admin.site.register(models.ModelName) in order to show the model in django admin.
You can find more about this in their official documentation
https://docs.djangoproject.com/en/2.2/ref/contrib/admin/

Django - registering an Admin model view in one app from another app

I'm slightly confused as how I'm meant to do this, partly because the documentation is translated a little poorly from Chinese, partly because I am just getting my head around class based views.
EDIT: I am using xadmin (drop in replacement of django admin) instead of the built-in django admin site.
I have the following directory structure:
Project
manage.py
db.sqlite3
/docs
/static
/templates
/main_app
__init__.py
settings.py
urls.py *
wsgi.py
/apps
/xadmin
adminx.py *
/survey
admin.py *
And the following URL mappings in main_app.urls:
from django.conf.urls import url, include
from django.contrib import admin
import xadmin
xadmin.autodiscover()
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^survey/', include('survey.urls')),
url(r'xadmin/', include(xadmin.site.urls)),
]
The survey.admin has it's classes, e.g.
class SurveyAdmin(admin.ModelAdmin):
list_display = ('name', 'is_published', 'need_logged_user', 'template')
list_filter = ('is_published', 'need_logged_user')
inlines = [CategoryInline, QuestionInline]
actions = [make_published]
admin.site.register(Survey, SurveyAdmin)
I know my Survey app's models are working properly, because when I check db.sql3, I can see my dummy entries in there. When I log into 127.0.0.1:8000/admin, I can see that the SurveyAdmin view is registered and available.
When I log into 127.0.0.1:8000/xadmin however, SurveyAdmin isn't registered.
From the xadmin docs I get that I have to register admin class views in xadmin.adminx. The admin class views I want to register already exist in survey.admin. I believe all I need to get this working is to move those views to xadmin.adminx - I really just want to check to make sure this is correct before I do so.
Shouldn't you add this also in urls.py:
from xadmin.plugins import xversion
xversion.register_models()
It's in the quickstart docs
Got it working. All I had to do was move the classes from survey.admin to xadmin.adminx and change the parameters slightly.
moved from survey.admin
class SurveyAdmin(admin.ModelAdmin):
list_display = ('name', 'is_published', 'need_logged_user', 'template')
list_filter = ('is_published', 'need_logged_user')
admin.site.register(Survey, SurveyAdmin)
to xadmin.adminx
class SurveyAdmin(object):
list_display = ('name', 'is_published', 'need_logged_user', 'template')
list_filter = ('is_published', 'need_logged_user')
xadmin.site.register(Survey, SurveyAdmin)
Makes sense as xadmin.adminx is meant to replace your usual admin.py (which survey.admin was extending previously)

How to keep using 3rd app admin templates after customizations of admin.py?

I use django-photologue and extended it:
# gallery.models.py
from photologue.models import Photo
from profiles.models import UserProfile
class PhotoExtended(Photo):
user = models.ForeignKey(UserProfile, verbose_name=_('user'), on_delete=models.CASCADE)
# gallery.admin.py
from photologue.admin import PhotoAdmin as PhotoAdminDefault
from photologue.models import Photo
from .models import PhotoExtended
class PhotoAdmin(PhotoAdminDefault):
save_on_top = True
admin.site.unregister(Photo)
admin.site.register(PhotoExtended, PhotoAdmin)
Photologue has a feature to upload zip file with photos and it can be done using additional button in admin. After my changes this button disappeared.
Is it possible to use native photologues admin templates in order to avoid copy-pasting them to my app's template folder? In INSTALLED_APPS photologue is higher than my gallery app
Here there are the photologues admin templates.
In the path templates/admin/photologue/photo/change_list.htmlthe part photo corresponds to the Photo model. You subclassed that model. The new model name is PhotoExtended but there is no template in templates/admin/photologue/photo_extend/change_list.html.
Copy the change_list.html from photologue app into your own (app) template folder. Eg: project/app/templates/admin/photologue/photo_extend/change_list.html.
Alternatively you can also just create a new file and use include the old template:
# project/app/templates/admin/photologue/photo_extend/change_list.html
{% include 'admin/photologue/photo/change_list.html' %}
Update: Another alternative is to set (one of) the BaseModelAdmin properties:
# Custom templates (designed to be over-ridden in subclasses)
add_form_template = None
change_form_template = None
change_list_template = None
delete_confirmation_template = None
delete_selected_confirmation_template = None
object_history_template = None

Import Error Django-profiles

I'm trying to get the django-profiles work.
I follow the steps of this manual ("The Missing Manual"),
so:
I work in the same project I also use for the django-registration (so no app created!!!)
In my templates I've created a folder "/profiles" (including edit_profile.html)
My AUTH_PROFILE_MODULE is set to 'myProjectName.UserProfile'
I've created in my project folder models.py containing:
from django.db import models
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
first_name = models.CharField(_('first name'), max_length=100)
middle_name = models.CharField(_('middle name'), blank=True, max_length=100)
last_name = models.CharField(_('last name'), max_length=100)
birth_date = models.DateField(_('birth date'), blank=True, null=True)
In my project folder, I've created forms.py, with exactly the same code as in the manual
In urls.py of my project I've added:
from myProjectName.forms import ProfileForm
('^profiles/edit', 'profiles.views.edit_profile',{'form_class':ProfileForm,'success_url':'/my/custom/url',}),
(r'^profiles/', include('profiles.urls')),
So now the whole urls.py file of my project is:
from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template
from django.contrib import admin
from myProjectName.forms import ProfileForm
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/', admin.site.urls),
(r'^accounts/', include('registration.urls')),
(r'^$', direct_to_template,
{ 'template': 'index.html' }, 'index'),
('^profiles/edit', 'profiles.views.edit_profile', {'form_class': ProfileForm,'success_url':'/my/custom/url',}),
(r'^profiles/', include('profiles.urls')),
)
When I access now http://127.0.0.1:8000/profiles/edit/, I get this error:
Exception Value: No module named ourcrestmont.itaco.models
That's the import located in forms.py ...
What am I doing wrong?
EDIT:
The code of my forms.py:
from django.db import models
from django.forms import ModelForm
from ourcrestmont.itaco.models import *
class ProfileForm(ModelForm):
class Meta:
model = Foo
exclude = ('field1','field2','field3',)
It's exactly the same code as in the manual, or is this code not good? (could anyone give an other, better code?)
So that import statement appears to be attempting to import a file called "ourcrestmont/itaco/models.py". Make sure that exists.
(by django model, that looks like it is a project called ourcrestmont with an app called itaco)
If it exists, make sure that itaco and ourcrestmont both have init.py files in them.
You can just have the project (the import line in that case is from projectname.models import modelname) but I'd recommend sticking to the standard layout and putting your models, forms and views together in an App beneath a project, even if you only currently see your project as having one App. (Because it doesn't, it has at least three, since registration and profiles are also django apps, they're just django apps outside of what you've made, if you see what I mean, and thinking of it like that might make dependancy and inclusion bugs easier to solve. Certainly every other django app is going to assume most things are inside an app)