'how send a post request in a select tag in html, how should i provide name="" and value="" in this tag like in <input>, also how to change date format
I have two problems right now, one is getting a [post request with teh value of selec tag and also how to change teh date format in django in DD/MM/YYYY format with same
html file
<div class="form-group">
<label class="my-1 mr-2" for="inlineFormCustomSelectPref">Author</label>
<select class="custom-select my-1 mr-sm-2" id="inlineFormCustomSelectPref">
<option selected>Choose...</option>
{% for bks in book %}
<option value="$">{{bks.Author}}</option>
{% endfor %}
</select>
</div>
models.py
class Books(models.Model):
Name = models.CharField(max_length=250)
Author = models.ForeignKey('Authors', on_delete=models.CASCADE, default=None)
Published_Date = models.DateField(blank=False)
Pages = models.IntegerField()
critics = models.IntegerField(default=0)
def __str__(self) -> str:
return self.Name
views.py
@api_view(['GET','POST'])
def Book_list(request):
"""
List all code snippets, or create a new snippet.
"""
if request.method == 'GET':
authors = Books.objects.all()
serializer = BookSerializer(authors, many=True)
context = {
'book':authors
}
print(context)
return render(request, 'book.html', context)
elif request.method == 'POST':
serializer = BookSerializer(data=request.data)
# checking if serializer is valid
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
serializer.py
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Books
fields = ['Name', 'Author', 'Published_Date', 'Pages', 'critics']
any help will be appreciated
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
