I am new to Django and need some help.
I am basically trying to build a Tinder-like application for movies using Django and working on the basics of the 'swiping' functionality. And, while using a form I am having trouble getting the swipe input. I only want a boolean value from the user (true for yes and false for no) and for this to get updated according to the movie id in the database. But I am unable to perform this updation. My form adds a new record instead.
Perhaps an easier alternative would be to remove the swipes field from the model entirely , just use it as a variable and maintain a list of movies where this variable was selected to be true. I was unable to access the movie id in this case so this failed too ;-;
How do I get this working in a simple efficient manner?
Here is what my models.py looks like:
class Movie(models.Model):
movie_name = models.CharField(max_length=300) # unique id for room
movie_description = models.TextField(default='')
movie_genre = models.CharField(max_length=100)
movie_date_released = models.DateField(null=True)
movie_swiped = models.BooleanField(default = False)
# Override the __str__ method to return the firstname and lastname
def __str__(self):
return self.movie_name
def is_exists(self):
''' Check whether a user exists in the database '''
if Movie.objects.get(movie_name=self.movie_name):
return True
else:
return False
views.py
# Create your views here.
class RoomSwipeView(APIView):
# Define a class variables
serializer_class = MovieSerializer
queryset = []
swiped_movies=[]
def get(self, request):
# Get the room id from the request
movie_id = request.GET.get('id')
if movie_id:
queryset = Movie.objects.filter(id=movie_id)
else:
queryset = Movie.objects.all()
print(queryset)
print("BRUHHHH")
form = MovieSwipeForm
return render(request,'swipe.html',{'details':queryset, 'form':form})
def post(self, request):
form = MovieSwipeForm(request.POST)
submitted = False
if request.method == "POST":
if form.is_valid():
print("Getting here!!!!!!!")
swiped = form.save(commit=False)
if(swiped.movie_swiped):
swiped_movies.push(swiped)
print(swiped_movies)
else:
form = MovieSwipeForm
if 'submitted' in request.GET:
submitted = True
return render(request,'swipe.html',{'details':queryset, 'form':form,'submitted':submitted})
Another issue in this file is that the swiped_movies
and queryset
class variables are not being accessed by the post method and that throws an error too.
swipe.html
<html>
<body>
<h1> Movie Details</h1>
{% for obj in details %}
{{obj.movie_name}}<br/>
Description: {{obj.movie_description}}<br/>
Genre: {{obj.movie_genre}}<br/>
Date Released: {{obj.movie_date_released}}<br/>
<!-- <button type="button">YES</button>
<button type="button">NO</button> -->
{% if submitted %}
Success
{% else %}
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
<!-- <input type="submit" value="NO"> -->
</form>
{% endif %}
<br/><br/>
{% endfor %}
</body>
</html>
forms.py
class MovieSwipeForm(ModelForm):
class Meta:
model = Movie
fields = ('movie_swiped',)
You need to pass instance of movie object which you want to update it to form when initiate the form, thereby an existence object will be updated, otherwise form.save() will create new object instead
In view.py
Instance = Movie.objects.get(id=movie_id)
form = MovieSwipeForm(request.POST, instance=instance)
To access class parameters in the class, you need to call them self.parameter so to access queryset in post method use self.queryset
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.