Posts

Showing posts with the label Field

ImageField overwrite image file with same name

The default behavior of the Django ImageField when you upload a new image has the same name with an existing image Django will add suffix to that new one: existingname_<somesuffix>.JPG But if the image names are identical and represent each object in the database (named after the object's pk), you don't want to have that suffix added. You just want to remove the existing one with the new photo. If that the case, you can define your storage class and assign to the ImageField in models.py like the following example: 1. Create a file name storage.py and put it in the same place with settings.py: from django.core.files.storage import FileSystemStorage from django.conf import settings import os class OverwriteStorage( FileSystemStorage):     def get_available_name(self, name):         """Returns a filename that's free on the target storage system, and         available for new content to be written to.     ...

Assigning roles to users in Moodle based on their profile data using php script

In case you want to allow some particular blocks to be seen only by a particular group of users in your Moodle, you can do as below: Note: assuming you've already created all the user accounts in your Moodle. 1. Categorize your users using some fields like department or description: For example, I have Middle School Students and High School Students. Each of the students has Graduation Year information stored in the department field of the user profile. I will then use that field to determine which school they are studying now: Student Number 1's department: 2015 class => High School Student Number 2's department: 2021 class => Middle School The below snippet is pretty handy to identify which school a student is studying based on her graduation year: <?php function class_to_grade_level ($class_of) { // you may want to change the timezone date_default_timezone_set('Asia/Ho_Chi_Minh'); $grad_year = intval($class_of); $curr...

Django - Create a form with read-only field

To Create a read-only field for a form in Django: somefield = forms . CharField ( widget = forms . TextInput ( attrs ={ 'readonly' : 'readonly' }) ) Example:  class AddStudentAccountForm(forms.Form):          first_name = forms.CharField(max_length=100)          last_name = forms.CharField(max_length=100)          unique_id = forms.CharField(max_length=10)          description = forms.CharField(max_length=255, initial='Students <Grad Year>', widget=forms.TextInput(attrs={'readonly':'readonly'}))          department = forms.CharField(max_length=255, initial='SSIS Students', widget=forms.TextInput(attrs={'readonly':'readonly'}))          grad_year = forms.ChoiceField(choices=YEARS)

Django form - Add placeholder text to a form field

Image
Here is how I add placeholder text to a Django form field, for example: from django import forms class SearchForm(forms.Form): email = EmailField(widget=forms.TextInput(attrs={'placeholder': 'Email'})) In the template: {{ search_form.email }} It will be rendered as: