CORS policy issue when using with JSON and DJANGO
Hi Team, I am making quiz app, for which i wrote a function in view which will generate the json file and place it under the same location as of my all teamplates (C:/Desktop/Environment/envQuiz/quizsetup/templates/quizsetup) Now I am trying to read this json file in my HTML page via javascript file, and getting error as attached "Issue1.PNG". Please help... I also tried using header.. but no luck. Below is the js code i am using. var requestURL = 'C:/Desktop/Environment/envQuiz/quizsetup/templates/quizsetup/json_data.json'; let questions; let questionsCount; let currentQuestion; let score = 0; let question_title_elem = document.getElementById("title"); let answers_elem = document.getElementById("answers"); let action_btn = document.getElementById("action_btn"); function getQuestions () { let request = new XMLHttpRequest(); request.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { questions = JSON.parse(this.responseText).questions; questionsCount = questions.length; currentQuestion = 0; } } /*json_data -- This is from view takeQuiz */ request.open("GET", requestURL, false); request.send(); . .. . . . ... -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/57fd8bf5-8056-4355-b1ec-77247445e48d%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Django - PrePopulate the Foreign Key on the web page, and use that id to save the model.
Hi All, I am facing an issue, to pre-populate the Foreign Key on the web page. Actually i have two models Projects and Modules. To create a Module, one has to select the Project and go to Module page for create it, but there the Project is not populated. Below are the details... Please guide me. *models.py* class Project(models.Model): name = models.CharField(max_length = 200, null = True) startdate = models.DateTimeField() def __str__(self): return self.name class Modules(models.Model): project = models.ForeignKey(Project, null = True, on_delete = models.SET_NULL) modulename = models.CharField(max_length = 200, null = True) modulestartdate = models.DateTimeField() def __str__(self): return self.modulename *forms.py* class ProjectForm(forms.ModelForm): class Meta: model = Project fields = '__all__' class ModuleForm(forms.ModelForm): class Meta: model = Modules fields = '__all__' *views.py* def projectView (request): if request.method == 'POST': form = ProjectForm(request.POST) if form.is_valid(): form.save(commit=True) return render(request, 'budget/projectForm.html', {'form': form}) else : form = ProjectForm() return render(request, 'budget/projectForm.html', {'form': form}) def modulesView (request, projectid): project = Project.objects.get(pk=projectid) if request.method == 'POST': form = ModuleForm(request.POST) if form.is_valid(): form.save(commit=True) return render(request, 'example/modulesForm.html', {'form': form}) else : form = ModuleForm(instance=project) return render(request, 'example/modulesForm.html', {'form': form}) *urls.py* path('testproject/', views.projectView, name='projectView'), path('testprojectmodule//', views.modulesView, name='modulesView'), in html forms for both Project and Modules... just using {{ form.as_table }}. Now after createing the Project, i used the url as http://127.0.0.1:8000/testprojectmodule/1/ then my Project should get pre-populated... as reached using the project id which is 1 in this case, but i am getting full drop-down. Refer attached image. Either the Project should be pre-selected.. or can make it non-editable either will work for me. Please help. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/046a4b4b-57c5-45f2-bfed-84f938cf1e2d%40googlegroups.com.
django dependent drop down list without using javascript; ajax; jquery
Hi All, Could any one please share some details on how we can do dependent drop down list selection. I am seeing lot of videos or content to handle this via JavaScript; AJAX; jquery etc.. but just wondering if Django has anything which can be handled easily. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5d0ab688-2f10-4380-b14d-c622ae5fa9fc%40googlegroups.com.
Django - What is the best approach to handle multiple user types…and route the HTML pages based on this?
I'm making a small test project with below user types: School Admin, Teacher, Student, Parent. And each user type will have different Permissions like School Admin has full access... Parents can only view their Childern's Activity. Teacher can see all students but can add / edit marks for their respective students only. Teachers can setup Exam etc.. Students can take exam and submit the exam, but cannot edit / add any other information. Just can edit his profile detail. Approach 1: Do i need to create a Custom User calss and apply to each user type (ie one Custome Class.. and 4 user type calss).. and similarly have 4 different view and html pages? Approach 2: Just have one custome class and have an field with UserType which will have the have as "SchoolAdmin", "Teacher", "Student","Parent".. and one view and html (as the data page would remain same and only data record will restrict), and somehow identify the User Type in View, and filter the record? Definately some view or html pages will be specific to one user type only which is i am able to handle, the issue is to use same view / html page to handle all user type. Please suggest... and any code snippet will will more helpful. Below is the thing from approach 1, i was trying. # models.py-class CustomUser(AbstractUser): USER_TYPE_CHOICES = ( ('SchoolAdmin'), ('Teacher'), ('Student'), ('Parents'), ) user_type = models.CharField(blank=False, choices=USER_TYPE_CHOICES) name = models.CharField(blank=False, max_length=255) country = models.CharField(blank=False, max_length=255) city = models.CharField(blank=False, max_length=255) phone = models.CharField(blank=True, max_length=255) created_at = models.DateField(auto_now_add=True) def __str__(self): return self.name class SchoolAdmin(models.Model): user = models.OneToOneField( CustomUser, on_delete=models.CASCADE, primary_key=True) class Teacher(models.Model): user = models.OneToOneField( CustomUser, on_delete=models.CASCADE, primary_key=True) photo = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True) class Student(models.Model): user = models.OneToOneField( CustomUser, on_delete=models.CASCADE, primary_key=True) teacher = models.ForeignKey(Teacher) photo = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True) class Parent(models.Model): user = models.OneToOneField( CustomUser, on_delete=models.CASCADE, primary_key=True) student= models.ForeignKey(Student) -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/dbc99b6e-3d46-41c4-a5a9-1e85bbf7d8a4%40googlegroups.com.
Django Type Error with POST method
Hi All, I am working on small hands-on project.. and having an issue when calling the POST method with form.py Below is the code. Here i have two models.. one is Category and another is SubCategory. For SubCategory i have created form "SubCategoryFormv2" in forms.py... where i am checking if categoryid is passed then pre-select that Category, else display all Category for selection. On submit the form from html... i am getting TypeError. (full error is displayed below) Please help... What i could identify is with the POST method, when the statement *form = SubCategoryFormv2(request.POST)* is executed, it is expecting some id.. not sure what is this id. # models.py class SubCategory(models.Model) : category = models.ForeignKey(Category, null = True, on_delete = models.SET_NULL) name = models.CharField(max_length = 200, null = True) dateCreated = models.DateTimeField(default = timezone.now) createdBy = models.ForeignKey(User, on_delete = models.PROTECT ) # forms.py -- class SubCategoryFormv2(forms.ModelForm): class Meta: model = SubCategory fields = '__all__' widgets = {'dateCreated' : DateInput()} def __init__(self, categoryid=None, *args, **kwargs): super(SubCategoryFormv2, self).__init__(*args, **kwargs) if categoryid is not None: self.fields['category'] = forms.ModelChoiceField(initial=categoryid, queryset=Category.objects.filter(id=categoryid)) # views.py def createSubCategoryV2(request, categoryid=None): context = { 'title':'Add SubCategory' } if request.method =='POST': print('POST METHOD') form = SubCategoryFormv2(request.POST) print('POST METHOD - SubCategoryFormv2 called') if form.is_valid(): ins = form.save(commit=True) print('Inside for form validation') messages.success(request, f'SubCategory added Successfully') return redirect('listsubcategory') else: # for GET method. print('GET METHOD') form = SubCategoryFormv2(categoryid) context["form"]= form return render(request, 'DailyBudget/subcategory_form.html', context) Error : Exception Value: Field 'id' expected a number but got . -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1fa99634-a1cb-477c-8972-19f23d7f2284%40googlegroups.com.
overriding __init__ in forms.ModelForm is erroring with POST method
Hi All, I am working on small hands-on project.. and having an issue when calling the POST method with form.py overriding __init__. Below is the code. Here i have two models.. one is Category and another is SubCategory. For SubCategory i have created form "SubCategoryFormv2" in forms.py... where i am checking if categoryid is passed then pre-select that Category, else display all Category for selection. On submit the form from html... i am getting TypeError. (full error is displayed below) Please help... What i could identify is with the POST method, when the statement *form = SubCategoryFormv2(request.POST)* is executed, it is expecting some id.. not sure what is this id. # models.py class SubCategory(models.Model) : category = models.ForeignKey(Category, null = True, on_delete = models.SET_NULL) name = models.CharField(max_length = 200, null = True) dateCreated = models.DateTimeField(default = timezone.now) createdBy = models.ForeignKey(User, on_delete = models.PROTECT ) # forms.py -- class SubCategoryFormv2(forms.ModelForm): class Meta: model = SubCategory fields = '__all__' widgets = {'dateCreated' : DateInput()} def __init__(self, categoryid=None, *args, **kwargs): super(SubCategoryFormv2, self).__init__(*args, **kwargs) if categoryid is not None: self.fields['category'] = forms.ModelChoiceField(initial=categoryid, queryset=Category.objects.filter(id=categoryid)) # views.py def createSubCategoryV2(request, categoryid=None): context = { 'title':'Add SubCategory' } if request.method =='POST': print('POST METHOD') form = SubCategoryFormv2(request.POST) print('POST METHOD - SubCategoryFormv2 called') if form.is_valid(): ins = form.save(commit=True) print('Inside for form validation') messages.success(request, f'SubCategory added Successfully') return redirect('listsubcategory') else: # for GET method. print('GET METHOD') form = SubCategoryFormv2(categoryid) context["form"]= form return render(request, 'DailyBudget/subcategory_form.html', context) Error : Exception Value: Field 'id' expected a number but got . -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d1fb0a49-c582-4623-b391-9f1e54e57c3f%40googlegroups.com.
Can we use Database with Django without creating models
Hi Team, I am trying to get options to connect to Database from Python-Django App. But here the requirement is I already have a Database setup (MySQL) with all required tables and fields. So am not willing to use models.py and create tables. Is there a way still i can use the tables... and still be able to do similar query which is done using ORM (i mean Django default querysets). Please suggest. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ea4fdb96-0462-40ad-aec1-6bf1318cb0fd%40googlegroups.com.
Customer premissions based on Membership plan
Hi All, I am working on hands-on Django web application, and for this i have to provide access to some videos; products based on the Customer Membership plan. Like if a Customer is in Basic Plan, then allow only to view 10 videos or so.. and for Permium Customer, no limit. For such type of requirement, i was trying to create a model class as Customer_membership which will be a one-to-one relationship with my custom Customer model class. Please suggest, on view and templates how will i be restrict the customer based on their plan. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/80533c17-f969-4462-b8bb-dda76cac5ec2n%40googlegroups.com.
Django django_filters DateFilter customization
Hi All, I am look for a solution where I can restrict the user to select date from current month only instead of selecting any other month or year. Currently user can select past month as well, have to restrict for current month only in date picker. Please help. Below is the code. filters.py file screenshot is as below. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b44d3a63-7f42-4ce5-9b6a-0a57e412f9cdn%40googlegroups.com.