How to create drop down in django dynamically , I tried all ways but I am not getting , literally every way passing from views and from models
views.py
@user_passes_test(is_manager)
def fetch_set(request, book_id):
set_list = []
clicked_book = Book.objects.get(pk=book_id)
url = clicked_book.url.split("/")[4].split(".")[0]
try:
repo = requests.get("https://api.github.com/repos/{}/{}/branches".format(github_user, url), headers = headers, proxies=proxy)
repo = json.loads(repo.text)
for branch in repo:
set_list.append(branch['name'])
except Exception as e:
messages.error(request, e)
context = {
'set': SetCreationForm(initial={'repo_name':url, 'acronym': set_list , \
'book':clicked_book}),
'list': set_list,
'sets': Set.objects.filter(pk=book_id),
'id': book_id,
}
if request.method == 'POST':
form_1 = SetCreationForm(request.POST)
context = {'set': form_1, 'id': book_id}
try:
if form_1.is_valid():
form_1.save()
messages.success(request, 'Set is fetched.')
return redirect('booksNsets:sets-page')
except Exception as e:
# messages.WARNING(request, e)
form_1.add_error(None, e)
return render(request, 'booksNsets/fetch_repo.html', context)
return render(request, 'booksNsets/fetch_repo.html', context)
models.py
class Set(models.Model):
class StatusChoice(models.TextChoices):
READY_FOR_CORRECTION = 'RC', _('Ready for Corrrection')
UNDER_CORRECTION = 'UC', _('Under Corrrection')
READY_FOR_VERIFICATION = 'RV', _('Ready for Verification')
UNDER_VERIFICATION = 'UV', _('Under Verification')
READY_FOR_FORMATTING = 'RF', _('Ready for Formatting')
UNDER_FORMATTING = 'UF', _('Under Formatting')
READY_FOR_PROOFREADING = 'RP', _('Ready for Proofreading')
UNDER_PROOFREADING = 'UP', _('Under Proofreading')
COMPLETED_PROOFREADING = 'CP', _('Completed Proofreading (Mark set as completed for finalize)')
COMPLETED = 'CD', _('Completed')
book = models.ForeignKey(
Book,
related_name='sets_of_book',
on_delete=models.CASCADE,
null=True,
)
acronym = models.CharField(max_length=25, unique=True,null=True,blank=True)
repo_name = models.CharField(max_length=40)
start_page = models.IntegerField()
end_page = models.IntegerField()
notes = models.TextField(null=True, blank=True)
# Cache the completion status of the set
is_completed = models.BooleanField(default=False, blank=True)
class Meta:
ordering = ('acronym', )
def __str__(self):
return self.acronym
def clean(self):
if self.start_page > self.end_page:
raise ValidationError( 'Please check the page range.')
def get_status(self):
# Use cached status to check if set is completed.
if self.is_completed:
return self.StatusChoice.COMPLETED
# No assignment then READY_FOR_CORRECTION
if not self.assignments_for_set.count():
return self.StatusChoice.READY_FOR_CORRECTION
# Get active assignment
active_assign = self.assignments_for_set.filter(is_active=True).first()
if active_assign:
if active_assign.stage == Assignment.StageChoice.CORRECTION:
return self.StatusChoice.UNDER_CORRECTION
if active_assign.stage == Assignment.StageChoice.VERIFICATION:
return self.StatusChoice.UNDER_VERIFICATION
if active_assign.stage == Assignment.StageChoice.FORMATTING:
return self.StatusChoice.UNDER_FORMATTING
if active_assign.stage == Assignment.StageChoice.PROOFREADING:
return self.StatusChoice.UNDER_PROOFREADING
# Else get last completed assignment
last_assign = self.assignments_for_set.filter(is_last_completed=True).first()
if last_assign.stage == Assignment.StageChoice.CORRECTION:
return self.StatusChoice.READY_FOR_VERIFICATION
if last_assign.stage == Assignment.StageChoice.VERIFICATION:
return self.StatusChoice.READY_FOR_FORMATTING
if last_assign.stage == Assignment.StageChoice.FORMATTING:
return self.StatusChoice.READY_FOR_PROOFREADING
if last_assign.stage == Assignment.StageChoice.PROOFREADING:
return self.StatusChoice.COMPLETED_PROOFREADING
def mark_completed(self):
active_assign = self.assignments_for_set.filter(is_active=True).first()
if active_assign:
active_assign.mark_completed()
self.is_completed = True
self.save()
def get_total_pages(self):
return self.end_page - self.start_page + 1
def get_corrector_assignments(self):
return self.assignments_for_set.filter(stage=Assignment.StageChoice.CORRECTION)
def get_verifier_assignments(self):
return self.assignments_for_set.filter(stage=Assignment.StageChoice.VERIFICATION)
def get_formatter_assignments(self):
return self.assignments_for_set.filter(stage=Assignment.StageChoice.FORMATTING)
def get_proofreader_assignments(self):
return self.assignments_for_set.filter(stage=Assignment.StageChoice.PROOFREADING)
def get_active_assignment(self):
return self.assignments_for_set.filter(is_active=True).first()
forms.py
class SetCreationForm(forms.ModelForm):
class Meta():
model = Set
fields = ('book', 'repo_name','acronym',
'start_page', 'end_page', 'notes')
BRANCH_CHOICES = [tuple([x,x]) for x in 'acronym']
widgets = {'notes': forms.Textarea(attrs={
'rows': 6, 'cols': 35,
'style': 'resize:none;'})
}
acronym = forms.ChoiceField(widget = forms.ChoiceField(attrs={
'acronym': BRANCH_CHOICES}))
def clean(self):
cleaned_data = self.cleaned_data
# check for repo existance
try:
repo = g.get_repo(f"{github_user}/{cleaned_data['repo_name']}")
except:
raise ValidationError('Repository does not exists.')
return cleaned_data
THERE ARE views.py,models.py,forms.py and I have tried every single possibility and I understood how it is working but I am getting some or the other error please do help me with the code and give exactly what code should I change and write it because I tried it every way but I am not getting .
I want the acronym tag in drop down
Please see this
I am getting like this in a list but I want it in drop down
PIC:- SEE THIS OUTPUT