django datepicker format change - django

Im trying to change the datepicker format from mm/dd/yyyy to dd-mm-yyyy. So far I've added DATE_INPUT_FORMATS in settings.py to ['%d-%m-%Y'], but this didnt change anything. I also changing it via jquery based on proposed solution in this site but it didnt help also.
<script type="text/javascript">
$(function () {
$('#datepicker').datepicker({ dateFormat: 'dd-mm-yy' });
});
</script>
Its quite a trivial problem but I cant seem to find a way how to solve it.
forms.py
class Calendar(forms.DateInput):
input_type = 'date'
class AdForm(forms.ModelForm):
time_limit = forms.DateInput(format=['%d-%m-%Y'])
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['time_limit'].widget.attrs.update({'class': 'form-control mb-3 datepicker'})
class Meta:
model = Ad
fields = ('time_limit')
widgets = {
'time_limit': Calendar(),
}
The result is still as such:

You can customize the DATE_INPUT_FORMATS creating a formats folder under your main project dir and then adding a custon format file for each language you need as describer in the official Django doc.
You can see how to customize the FORMAT_MODULE_PATH and what is the full list of the (editable) available formats here.
Sample folder structure to edit the english format:
mysite/
formats/
__init__.py
en/
__init__.py
formats.py
And then the formats.py
DATE_FORMAT = 'j N, Y'
In the settings.py file
LANGUAGE_CODE = 'en-us'
USE_L10N = True
FORMAT_MODULE_PATH = [
'my_app.formats',
]

Related

Django Sitetree returning "expected str instance, LazyTitle found" in {% sitetree_page_title from menu %}

Django version: 3.2.9 & 4.0.4
Python version:3.8.10
OS: Ubuntu 20.04lts
I have a sitetree that is using a context variable in the title. When going to that path and loading the template with {% sitetree_page_title from menu %} it returns sequence item 1: expected str instance, LazyTitle found.
I didn't find any information on this exact issue, but it seems that when using context data in the title a LazyTitle object is generated. The object is not being converted to a string so the template engine is bombing.
I will include the relevant code below. I was able to get around the issue by editing sitetreeapp.py and wrapping the return of get_current_page_title() in str(), but that feels excessive.
The app in question is a small test app I'm working to layout an example CRUD app with the features our company needs. It does nothing fancy. The app isn't 100% complete yet so if anything looks out of place, it could be I haven't got to that point, but this portion should be working fine.
The sitetree in question is loaded dynamically via the config.ready method. Menu items without context variables are working without issue. I have verified that the context variable in question is available within the template. Hopefully it's something simple that I am overlooking. Any input is appreciated. I should also note that, while I have used StackOverflow for many years, I haven't posted much so please forgive my post formatting.
sitetree.py - note insert and list are working fine
from sitetree.utils import tree, item
sitetrees = [[
# setup the base tree
tree('app_model_test', items=[
# Then define items and their children with `item` function.
item('App Test', 'test_app:app_test_home',
hint='Test CRUD App',
children=[
item('Search Results', 'test_app:app_test_list',
in_menu=False, in_sitetree=False),
item('Insert Record', 'test_app:app_test_insert',
hint="Insert a new record",
),
item('Update Record {{ object.ssn }}', 'test_app:app_test_update object.id',
in_menu=False, in_sitetree=False),
])
]),
], ]
urls.py
app_name = 'test_app'
urlpatterns = [
path('insert/', views.app_test_form, name='app_test_insert'),
path('update/<int:id>/', views.app_test_form, name='app_test_update'),
path('list/', views.appTestListView.as_view(), name='app_test_list'),
]
views.py - relevant pieces
def app_test_form(request, action='add', id=0):
if id > 0:
test_obj = get_object_or_404(appTestModel, id=id)
form = appTestForm(instance=test_obj)
if request.method == 'GET':
if id == 0:
form = appTestForm()
test_obj = None
else:
if id == 0:
form = appTestForm(request.POST)
if form.is_valid():
form.save()
return redirect('/list')
# this will have to be somewhere else and contain whatever app apps are
# installed to get all the menu entries
menus = ['app_model_test']
context = {'form': form,
'title': 'app Test',
'action': action,
'menus': menus,
'object': test_obj}
return render(request, 'CRUD_base.html', context)
dyn_tree_register.py
from sitetree.sitetreeapp import register_dynamic_trees, compose_dynamic_tree
from sitetree.utils import tree, item
from . import sitetree as app_test_tree
register_dynamic_trees(
[
compose_dynamic_tree(*app_test_tree.sitetrees),
],
# Line below tells sitetree to drop and recreate cache, so that all newly registered
# dynamic trees are rendered immediately.
reset_cache=True
)
apps.py
from django.apps import AppConfig
class AppModelTestConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'app_model_test'
def ready(self):
from . import dyn_tree_register
and lastly the relevant template portion:
{% load sitetree %}
{% for menu in menus %}
<h1 class="title" id="page-title">{% sitetree_page_title from menu %}</h1>
{% endfor %}
As I mentioned, I can make this work by modifying sitetreeapp.py and wrapping the return of get_current_page_title in str():
def get_current_page_title(self, tree_alias: str, context: Context) -> str:
"""Returns resolved from sitetree title for current page.
:param tree_alias:
:param context:
"""
return str(self.get_current_page_attr('title_resolved', tree_alias, context))
Edit - 2022-04-13
As a temporary workaround I created a wrapper template tag that calls the sitetree_page_title and then renders it and wraps the output in str(). This seems hackish to me so I appreciate any insight. Short term this will get me by but I would rather not put something like this into production as I feel there's a bug on my side rather than within sitetree otherwise more people would have run into this.
custom tag and template.Node class
#register.tag()
def app_str_sitetree_page_title(parser, token):
'''
There is an issue causing sitetree's page title function to return a LazyPageTitle object which is not being converted to a string.
This function is a temporary fix until the issue is resolved to force the return to a string.
'''
from sitetree.templatetags import sitetree
ret = sitetree.sitetree_page_title(parser, token)
return StrSitetreePageTitle(ret)
class StrSitetreePageTitle(template.Node):
'''
This is the render wrapper to ensure a string is returned from sitetree_page_title
Like app_str_sitetree_page_title this is temporary
'''
def __init__(self, title_obj) -> None:
self.title_obj = title_obj
def render(self, context):
title_render = self.title_obj.render(context)
return str(title_render)

django: download filtered data as csv

Hej!
I want to download my data as a csv file.
My problem is that I can't get my filters applyed in the downloaded csv, it'll always give me the whole list.
I tried to connect the two functions but it doesn't work as expected. But they both run fine on their own.
Does anyone know how to achieve that? Or knows what I'm doing wrong?
Any help is appreciated! :)
# views.py
def download_csv(request):
institutions = Institution.objects.all()
filter = InstitutionFilter(request.GET, queryset=institutions).qs
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="institutions.csv"'
writer = csv.writer(response)
writer.writerow(['Name', "Abbreviation", "Parent Institution", "Phone Number"])
for institution in filter.values_list('name', 'abbreviation', 'parent_institution__name', 'contact_details'):
writer.writerow(institution)
return response
# filters.py
class InstitutionFilter(django_filters.FilterSet):
name = CharFilter(field_name="name", lookup_expr="icontains")
class Meta:
model = Institution
fields = "__all__"
I had to add the filter parameter to the url to get them working. So the solution lies in the template rather then in the views :)
<div class="col">
Download CSV
</div>
EDIT:
#csv_download/urls.py
app_name = "csv_download"
urlpatterns = [
path("export-institution/", download_csv_institution, name="download_institutions"),
]
# general urls.py
urlpatterns = [
path("admin/", admin.site.urls, name="admin"), # admin site
path("download_csv/", include("csv_download.urls")), # download app
]

Cloudinary shown incorrect url in Django

I have created a model that uploads img to cloudinary, however, shows a wrong URL in the Django template which has 2 'https://' parts inside of it. Please help.
models.py:
class Product(models.Model):
img = models.ImageField(upload_to='product', height_field=None, width_field=None, max_length=100,
default='https://res.cloudinary.com/dgdhnbsae/image/upload/vxxxx/product/xxxx.jpg')
def get_img(self):
return f"{'/media/product/'+self.img}"
I have set the related configuration according to the tutorial
setting.py:
INSTALLED_APPS = [
xxxx
'cloudinary_storage',
'cloudinary',
]
CLOUDINARY_STORAGE = {
'CLOUD_NAME': 'xxxx',
'API_KEY': 'xxxxx',
'API_SECRET': 'xxxxx'
}
DEFAULT_FILE_STORAGE = 'cloudinary_storage.storage.MediaCloudinaryStorage'
Template:
<div class="featured__item__pic set-bg" data-setbg="{{ product.img.url }}">
The output:
https://res.cloudinary.com/xxxx/image/upload/v1/media/https://res.cloudinary.com/vxxxx/image/upload/xxxxx/xxxxx.jpg
So if I understood your problem correctly,
youll need to do the below changes
in your model.py file, add below
from cloudinary.models import CloudinaryField
instead of using the built in function for the image upload, you'd want to use the cloudinary function.
eg:
img = CloudinaryField('attributes')
you can later call this in views as usual and then pass it on to your django template. or you can use cloudinary template call, which will allow you to parse additional image manipulation options into the url string.
Some references:
https://jszczerbinski.medium.com/django-web-app-and-images-cloudinary-straightforward-study-ae8b5bb03e37
an easier reference:
https://alphacoder.xyz/image-upload-with-django-and-cloudinary/

Limit Amount Of Files A User Can Upload

I have a multi-file upload and want to limit users to 3 uploads each. My problem is that I need to know how many files a user has already created in the DB and how many they are currently uploading (they can upload multiple files at once, and can upload multiple times).
I have attempted many things, including:
Creating a validator (the validator was passed the actual file being added, not a model, so I couldn't access the model to get it's id to call if StudentUploadedFile.objects.filter(student_lesson_data=data.id).count() >= 4:).
Doing the validation in clean(self): (clean was only passed one instance at a time and the DB isn't updated till all files are cleaned, so I could count the files already in the DB but couldn't count how many were currently being uploaded).
Using a pre-save method (If the DB was updated between each file being passed to my pre-save method it would work, but the DB is only updated after all the files being uploaded have passed through my pre-save method).
My post-save attempt:
#receiver(pre_save, sender=StudentUploadedFile)
def upload_file_pre_save(sender, instance, **kwargs):
if StudentUploadedFile.objects.filter(student_lesson_data=instance.data.id).count() >= 4:
raise ValidationError('Sorry, you cannot upload more than three files')
edit:
models.py
class StudentUploadedFile(models.Model):
student_lesson_data = models.ForeignKey(StudentLessonData, related_name='student_uploaded_file', on_delete=models.CASCADE)
student_file = models.FileField(upload_to='module_student_files/', default=None)
views.py
class StudentUploadView(View):
def get(self, request):
files_list = StudentUploadedFile.objects.all()
return render(self.request, 'users/modules.html', {'student_files': files_list})
def post(self, request, *args, **kwargs):
form = StudentUploadedFileForm(self.request.POST, self.request.FILES)
form.instance.student_lesson_data_id = self.request.POST['student_lesson_data_id']
if form.is_valid():
uploaded_file = form.save()
# pass uploaded_file data and username so new file can be added to students file list using ajax
# lesson_id is used to output newly added file to corresponding newly_added_files div
data = {'is_valid': True, 'username': request.user.username, 'file_id': uploaded_file.id, 'file_name': uploaded_file.filename(),
'lesson_id': uploaded_file.student_lesson_data_id, 'file_path': str(uploaded_file.student_file)}
else:
data = {'is_valid': False}
return JsonResponse(data)
template.py
<form id='student_uploaded_file{{ item.instance.id }}'>
{% csrf_token %}
<a href="{% url 'download_student_uploaded_file' username=request.user.username file_path=item.instance.student_file %}" target='_blank'>{{ item.instance.filename }}</a>
<a href="{% url 'delete_student_uploaded_file' username=request.user.username file_id=item.instance.id %}" class='delete' id='{{ item.instance.id }}'>Delete</a>
</form>
js
$(function () {
// open file explorer window
$(".js-upload-photos").on('click', function(){
// concatenates the id from the button pressed onto the end of fileupload class to call correct input element
$("#fileupload" + this.id).click();
});
$('.fileupload_input').each(function() {
$(this).fileupload({
dataType: 'json',
done: function(e, data) { // process response from server
// add newly added files to students uploaded files list
if (data.result.is_valid) {
$("#newly_added_files" + data.result.lesson_id).prepend("<form id='student_uploaded_file" + data.result.file_id +
"'><a href='/student_hub/" + data.result.username + "/download_student_uploaded_file/" +
data.result.file_path + "' target='_blank'>" + data.result.file_name + "</a><a href='/student_hub/" + data.result.username +
"/delete_student_uploaded_file/" + data.result.file_id + "/' class='delete' id=" + data.result.file_id + ">Delete</a></form>")
}
}
});
});
UPDATE:
forms.py
class StudentUploadedFileForm(forms.ModelForm):
student_file = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))
view.py
class StudentUploadView(View):
model = StudentUploadedFile
max_files_per_lesson = 3
def post(self, request, *args, **kwargs):
lesson_data_id = request.POST['student_lesson_data_id']
current_files_count = self.model.objects.filter(
student_lesson_data_id=lesson_data_id
).count()
avail = self.max_files_per_lesson - current_files_count
file_list = request.FILES.getlist('student_file')
print(len(file_list))
if avail - len(file_list) < 0:
return JsonResponse(data={
'is_valid': False,
'reason': f'Too many files: you can only upload {avail}.'
})
else:
for f in file_list:
print(f)
data = {'test': True}
return JsonResponse(data)
Thank you.
I've tried using a PyPi package and it works flawlessly. I'm gonna go out on a limb here and assume that you are open to editing the package code to fix any errors you encounter due to compatibility issues since most of the packages that haven't been updated in quite a while might face them.
To solve the problem of limiting the number of files a user can upload, django-multiuploader package would be of enormous help and would honestly do more than you ask for. And yes, it uses JQuery form for uploading multiple files.
How to use it?
Installation and pre-usage steps
Installation
pip install django-multiuploader
python3 manage.py syncdb
python3 manage.py migrate multiuploader
In your settings.py file :
MULTIUPLOADER_FILES_FOLDER = ‘multiuploader’ # - media location where to store files
MULTIUPLOADER_FILE_EXPIRATION_TIME = 3600 # - time, when the file is expired (and it can be cleaned with clean_files command).
MULTIUPLOADER_FORMS_SETTINGS =
{
'default': {
'FILE_TYPES' : ["txt","zip","jpg","jpeg","flv","png"],
'CONTENT_TYPES' : [
'image/jpeg',
'image/png',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.ms-powerpoint',
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'application/vnd.oasis.opendocument.text',
'application/vnd.oasis.opendocument.spreadsheet',
'application/vnd.oasis.opendocument.presentation',
'text/plain',
'text/rtf',
],
'MAX_FILE_SIZE': 10485760,
'MAX_FILE_NUMBER':5,
'AUTO_UPLOAD': True,
},
'images':{
'FILE_TYPES' : ['jpg', 'jpeg', 'png', 'gif', 'svg', 'bmp', 'tiff', 'ico' ],
'CONTENT_TYPES' : [
'image/gif',
'image/jpeg',
'image/pjpeg',
'image/png',
'image/svg+xml',
'image/tiff',
'image/vnd.microsoft.icon',
'image/vnd.wap.wbmp',
],
'MAX_FILE_SIZE': 10485760,
'MAX_FILE_NUMBER':5,
'AUTO_UPLOAD': True,
},
'video':{
'FILE_TYPES' : ['flv', 'mpg', 'mpeg', 'mp4' ,'avi', 'mkv', 'ogg', 'wmv', 'mov', 'webm' ],
'CONTENT_TYPES' : [
'video/mpeg',
'video/mp4',
'video/ogg',
'video/quicktime',
'video/webm',
'video/x-ms-wmv',
'video/x-flv',
],
'MAX_FILE_SIZE': 10485760,
'MAX_FILE_NUMBER':5,
'AUTO_UPLOAD': True,
},
'audio':{
'FILE_TYPES' : ['mp3', 'mp4', 'ogg', 'wma', 'wax', 'wav', 'webm' ],
'CONTENT_TYPES' : [
'audio/basic',
'audio/L24',
'audio/mp4',
'audio/mpeg',
'audio/ogg',
'audio/vorbis',
'audio/x-ms-wma',
'audio/x-ms-wax',
'audio/vnd.rn-realaudio',
'audio/vnd.wave',
'audio/webm'
],
'MAX_FILE_SIZE': 10485760,
'MAX_FILE_NUMBER':5,
'AUTO_UPLOAD': True,
}}
Take note of that MAX_FILE_NUMBER, right within their lies the answer to your question. Have a look at the source once you install this and try implementing it on your own if you want. It might be fun.
Refer for further instructions :
django-multiuploader package on pypi
I guess that you can use multi file upload in Django hasn't trickled to the community yet. Excerpt:
If you want to upload multiple files using one form field, set the multiple HTML attribute of field’s widget:
# forms.py
from django import forms
class FileFieldForm(forms.Form):
file_field = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))
Your form and view structure is also very contrived, excluding fields from a form and then setting values injected via HTML form on the model instance. However, with the code shown, the model instance would never exist as the form has no pk field.
Anyway - to focus on the problem that needs fixing...
In the form, request.FILES is now an array:
class StudentUploadView(View):
model = StudentUploadedFile
max_files_per_lesson = 3
def post(request, *args, **kwargs):
lesson_data_id = request.POST['student_lesson_data_id']
current_files_count = self.model.objects.filter(
student_lesson_data_id=lesson_data_id
).count()
avail = self.max_files_per_lesson - current_files_count
file_list = request.FILES.get_list('student_file')
if avail - len(file_list) < 0:
return JsonResponse(data={
'is_valid': False,
'reason': f'Too many files: you can only upload {avail}.'
})
else:
# create one new instance of self.model for each file
...
Addressing comments:
From an aesthetic perspective, you can do a lot with styling...
However, uploading async (separate POST requests), complicates validation and user experience a lot:
The first file can finish after the 2nd, so which are you going to deny if the count is >3.
Frontend validation is hackable, so you can't rely on it, but backend validation is partitioned into several requests, which from the user's point of view is one action.
But with files arriving out of order, some succeeding and some failing, how are you going to provide feedback to the user?
If 1, 3 and 4 arrive, but user cares more about 1, 2, 3 - user has to
take several actions to correct the situation.
One post request:
There are no out of order arrivals
You can use a "everything fails or everything succeeds" approach, which is transparent to the end user and easy to correct.
It's likely that file array order is user preferred order, so even if you allow partial success, you're likely to do the right thing.
So the gist of it is that you are using jQuery .each to upload images via AJAX. Each POST request to your Django view is a single file upload, but there might be multiple requests at the same time.
Try this:
forms.py:
class StudentUploadedFileForm(forms.ModelForm):
class Meta:
model = StudentUploadedFile
fields = ('student_file', )
def __init__(self, *args, **kwargs):
"""Accept a 'student_lesson_data' parameter."""
self._student_lesson_data = kwargs.pop('student_lesson_data', None)
super(StudentUploadedFileForm, self).__init__(*args, **kwargs)
def clean(self):
"""
Ensure that the total number of student_uploaded_file instances that
are linked to the student_lesson_data parameter are within limits."""
cleaned_data = super().clean()
filecount = self._student_lesson_data.student_uploaded_file.count()
if filecount >= 3:
raise forms.ValidationError("Sorry, you cannot upload more than three files")
return cleaned_data
views.py:
class StudentUploadView(View):
def get(self, request):
# stuff ...
def post(self, request, *args, **kwargs):
sld_id = request.POST.get('student_lesson_data_id', None)
student_lesson_data = StudentLessonData.objects.get(id=sld_id)
form = StudentUploadedFileForm(
request.POST,
request.FILES,
student_lesson_data=student_lesson_data
)
if form.is_valid():
uploaded_file = form.save()
# other stuff ...

Django-cms PlaceholderField in custom plugin, used in Placeholder, not front-end editable

I am using the 2.2 django-cms on django 1.3, as well as the 2.3 django-cms git repo on django 1.4, and I notice that if I include a PlaceholderField in my model such as this:
##books/models.py
class Book(models.Model):
...
description = cmsmodels.PlaceholderField('book_description', null=True, blank=True)
class BookCMSPluginModelItem(models.Model):
t_book = models.ForeignKey(Book)
...
class BookCMSPluginModel(CMSPlugin):
featured_books = models.ManyToManyField(BookCMSPluginModelItem)
template = models.CharField(max_length=256, choices= settings.BOOKS_TEMPLATES, default=settings.BOOKS_TEMPLATES[0]);
and make my CMS Plugin like so:
##books/cms_plugins.py
class BookCMSPlugin(CMSPluginBase):
model = BookCMSPluginModel
name = _("Books Plugin")
def render(self, context, instance, placeholder):
self.render_template = instance.template;
context['instance'] = instance;
return context
Then in my template at some point I discover I can do the following:
{% for mitem in instance.featured_books.all %}
<!-- ...... -->
<div>{% render_placeholder mitem.t_book.description %}</div>
{% endfor %}
BUT, in the front end editor, I add my new plugin, and the description field does show up as a placeholder, however when I hover over it, the outer placeholder I placed the plugin into is active, and I cannot select directly the inner placeholder. It works quite well in the admin site, as I can add content using SemanticEditor and whatever else I wish to use. In the front-end, however, there's a frustrating issue with the javascript that seems to prevent the user from adding or editing the inner placeholder.
I should note that I followed the wonderful django-cms documentation here: http://docs.django-cms.org/en/latest/extending_cms/placeholders.html, but that site only addresses the issue in the context of displaying a model in a specifically designed page, unfortunately not using a template for a custom django-cms plugin.
In this thread on GitHub one of the django-cms developers says that there is no plugin-in-plugin architecture in Django CMS 2.2.
I've tried it with the latest development branch and it is working now.
Same issue with
Django==3.1.6
django-cms==3.8.0
python_version = 3.6.9
One unrecommended workaround is to change cms source code at 1 place
Go to virtualenvs/your-project-vitualenv/lib/python3.6/site-packages/cms/models
then edit the file placeholdermodel
got to line #123 Placeholder.has_change_permission and comment the first if-return.
=> Comment line #132 if not attached_models:
=> and line #136 return user.is_superuser
Save it and restart your server.
def has_change_permission(self, user):
"""
Returns True if user has permission
to change all models attached to this placeholder.
"""
from cms.utils.permissions import get_model_permission_codename
attached_models = self._get_attached_models()
# Comment this part!
# if not attached_models:
# # technically if placeholder is not attached to anything,
# # user should not be able to change it but if is superuser
# # then we "should" allow it.
# return user.is_superuser
attached_objects = self._get_attached_objects()
for obj in attached_objects:
try:
perm = obj.has_placeholder_change_permission(user)
except AttributeError:
model = type(obj)
change_perm = get_model_permission_codename(model, 'change')
perm = user.has_perm(change_perm)
if not perm:
return False
return True