Reputation: 55
I'm making a django app in which the homepage shows list of internships available of different companies.A user of usertype=Student can view those internships. Each internship list view has a button named 'Apply' when apply is clicked I want the booleanfield in my django model(StudentApplying) named applied to be set to true. So that the company who created this post can view the list of students who applied and then can accept/reject them. I don't want to use ajax,javascript etc unless there is no way I can't do it with django itself.
models.py
from django.db import models
from InternsOnboardMain.models import internshipPost
from django.contrib.auth.models import User
class StudentApplying(models.Model):
companyName =
models.ForeignKey(internshipPost,on_delete=models.CASCADE)
studentName = models.ForeignKey(User,on_delete=models.CASCADE)
applied = models.BooleanField(default=False)
view.py(I've tried this but it is not working)
from django.shortcuts import render, redirect,get_object_or_404
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from .models import StudentApplying
from django.contrib.auth.models import User
from django.db.models import F
def applyStatus(request):
if request.GET.get('applybtn'):
profil = get_object_or_404(StudentApplying,
created_by=request.user)
profil.applied = True
profil.save(update_fields=["applied"])
return redirect('InternsOnboard-Home')
return HttpResponse('Not done')
I have not used any forms.py
html file
<form method="POST" action="{% url 'Student-Apply' %}">
{% csrf_token %}
<button class="btn btn-info" name="applybtn">Apply</button>
</form>
There is no necessity to have tags
Upvotes: 0
Views: 1277
Reputation: 1211
Checking this a little more, the problem looks like that you try to submit an empty form. So you should try to do the following:
in the view:
if “applybtn” in request.POST:
# do this and that
The name of the button will be in the request thus it should work.
Upvotes: 2