django form template does not update - django

I have created a form and I POST selection with an action to go to specific url. Although, once directed the template does not update.
I need to hit Enter again and then obviously my request is lost so I get exception value get_context_data() takes exactly 2 arguments (1 given)
any ideas why ?
Below is my code and print screen of the problem.
url
url(r'^all_rooms/$', views.AllRoomsView.as_view(), name='roomsdetails'),
template
<form action="/blocks/all_rooms/" method="post" >
{% csrf_token %}
<ul>
{% for choice in form.my_choice_field.field.choices %}
<li>
<input type="radio" name="my_choice_field" value="{{choice.0}}"
{% ifequal form.my_choice_field.data choice.0 %}
checked="checked"
{% endifequal %}/>
<label for="">{{choice.1}}</label>
</li>
{% endfor %}
</ul>
<input type="submit" value="Submit" />
</form>
EDIT:
Below is my AllRoomsView class. I suspect this is the class where the problem is.
class AllRoomsView(ListView):
template_name = 'prostats/roomsdetail.html'
queryset = Room.objects.all()
def get_context_data(self, request, **kwargs):
context = super(AllRoomsView, self).get_context_data(**kwargs)
context['rooms'] = Room.objects.all()
context['rlog'] = RoomLog.objects.all()
roomsdates = []
for r in context['rlog']:
if r not in roomsdates:
roomsdates.append(r.update_date)
roomextracted = []
for i in roomsdates:
if i not in roomextracted:
roomextracted.append(i)
context['roomextracted'] = roomextracted
choosend = '2016-02-16'
context['roomfiltersettime'] = RoomLog.objects.filter(update_date__lte = choosend)
return context

Related

Django redirecting after clicking Like button

I'm doing Like/Unlike system in my project. After clicking Like button should just redirect on the same page but updated with +1 like but it is not. Have looked on youtube videos with this system and they got it worked but I can't figure it out what is the problem with my code.
views.py
def like_view(request, pk):
cocktail = get_object_or_404(AddCocktails, id=request.POST.get('cocktail_id'))
liked = False
if cocktail.likes.filter(id=request.user.id).exists():
cocktail.likes.remove(request.user)
liked = False
else:
cocktail.likesadd(request.user)
liked = True
return HttpResponseRedirect(reverse('cocktails:cocktail-details', args=[str(pk)]))
class CocktailDetails(LoginRequiredMixin, DetailView):
model = AddCocktails
template_name = 'cocktails/cocktail-details.html'
def get_context_data(self, *args, **kwargs):
cocktail_data = AddCocktails.objects.filter(id=self.kwargs['pk'])
context = super().get_context_data(**kwargs)
stuff = get_object_or_404(AddCocktails, id=self.kwargs['pk'])
total_likes = stuff.total_likes
liked = False
if stuff.likes.filter(id=self.request.user.id).exists():
liked = True
context['cocktail_data'] = cocktail_data
context['total_likes'] = total_likes
context['liked'] = liked
return context
urls.py
path('cocktail-details/<int:pk>/', CocktailDetails.as_view(), name='cocktail-details'),
path('likes/<int:pk>/', like_view, name='likes'),
template
{% for cocktail in cocktail_data %}
<h4>Cocktail Name</h4>
{{ cocktail.cocktail_name }}
<h4>Cocktail Category</h4>
{{ cocktail.cocktails_category }}
<h4>Type of Glass</h4>
{{ cocktail.crockery_category }}
<h4>Method Category</h4>
{{ cocktail.method_category }}
<h4>Ingredients</h4>
{{ cocktail.ingredients }}
<h4>Execution</h4>
{{ cocktail.execution }}
<img src="{{ cocktail.image.url }}" width="350" height="350">
<form action="{% url 'cocktails:likes' cocktail.pk %}" method=POST>
{% csrf_token %}
{% if liked %}
<button type="submit" name="cocktail.id" value="{{ cocktail.id }}" class="btn unlike">Unlike
</button>
{% else %}
<button type="submit" name="cocktail.id" value="{{ cocktail.id }}" class="btn like">Like
</button>
{% endif %}
- {{ total_likes }} Likes
</form>
{% endfor %}
So after clicking the button the url shows "http://127.0.0.1:8000/likes/3" and page not found
Should I create template for like_view?
Is putting Like system good idea inside for loop? Because when I put it outside got error saying Reverse for 'likes' with arguments '('',)' not found. 1 pattern(s) tried: ['likes/(?P<pk>[0-9]+)$']
In your template in form you use name="cocktail.id"
Than in your view your do get_object_or_404(...id=request.POST.get('cocktail_id').
cocktail_id does not exist in your query, that's why you get HttpResponseNotFound.
You have to use your button name cocktail.id:
cocktail = get_object_or_404(AddCocktails, id=request.POST.get('cocktail.id'))
or maybe even simpler use pk parameter:
cocktail = get_object_or_404(AddCocktails, id=pk)

In django, how to use another model data from function based views

So I have a form that updates a key_instance object with a borrower. Currently my app needs the user to enter the name of the borrower, but I want it to display a dropdown list of data from another model the user model to select from, is there anyway to do this in a class based view? Here are my views.py and my template. What I was thinking is that I would like to use a get_list_or_404 on the user model and display it as a drop down list in the template and use that selection to populate the form field.
I manged to get the dropdown list to display in my template but I'm not sure as to how to save that value in my views.
Does anyone know if this is the right way or if this is doable? Thank you!!
views.py
def submit_key_request(request, pk):
"""
View function for renewing a specific keyInstance by admin
"""
key_inst=get_object_or_404(KeyInstance, pk=pk)
names = get_list_or_404(Users)
# If this is a POST request then process the Form data
if request.method == 'POST':
# Create a form instance and populate it with data from the request (binding):
form = UpdateKeyForm(request.POST)
# Check if the form is valid:
if form.is_valid():
# process the data in form.cleaned_data as required (here we just write it to the model due_back field)
key_inst.is_requested = True
key_inst.status = 'r'
key_inst.date_requested = datetime.date.today()
key_inst.borrower = form.cleaned_data['borrower']
key_inst.save()
# redirect to a new URL:
return HttpResponseRedirect(reverse('all-available-keys') )
# If this is a GET (or any other method) create the default form.
else:
form = UpdateKeyForm(initial={'borrower': 'N/A'})
return render(request, 'catalog/keyinstance_request_update.html', {'form': form, 'keyinst':key_inst})
template
{% extends "base_generic.html" %}
{% block content %}
<div class="wrapper">
<div class="centered"> <h1>Request Keys For Room: {{keyinst.roomkey}}</h1></div>
<div class="square-box">
<div class="square-content">
<form action="" method="post" >
{% csrf_token %}
<table style="display: inline-flex">
{{ form}}
</table>
<select name = 'name'>
{% for name in names %}
<option value="{{ name }}">{{ name }}</option>
{% endfor %}
</select>
<p>
(Please use their login name i.e. <b>{{ user.get_username }}</b>)
</p>
<p><input required id="checkBox" type="checkbox" onclick="validate()"> I accept the terms and conditions</p>
<p id="text" style="display:none">You Have Agreed To the Terms and Conditions</p>
<input type="submit" value="Submit" />
</form>
</div>
</div>
</div>
{% endblock %}
Here is how I manged to do it, Not sure if this is the best 'pythonic' or best practice. Please let me know if it's not.
my views.py
def submit_key_request(request, pk):
"""
View function for renewing a specific keyInstance by admin
"""
key_inst=get_object_or_404(KeyInstance, pk=pk)
names = get_list_or_404(User)
# If this is a POST request then process the Form data
if request.method == 'POST':
name = request.POST['name']
key_inst.is_requested = True
key_inst.status = 'r'
key_inst.date_requested = datetime.date.today()
key_inst.borrower = name
key_inst.save()
return HttpResponseRedirect(reverse('all-available-keys') )
# If this is a GET (or any other method) create the default form.
else:
pass
return render(request, 'catalog/keyinstance_request_update.html', {'keyinst':key_inst, 'names':names})
template
{% extends "base_generic.html" %}
{% block content %}
<div class="wrapper">
<div class="centered"> <h1>Request Keys For Room: {{keyinst.roomkey}}</h1></div>
<div class="square-box">
<div class="square-content">
<form action="" method="post" >
{% csrf_token %}
</br>
<select name = 'name' required>
{% for key in names %}
<option value="{{ key }}">{{ key }}</option>
{% endfor %}
</select>
<p>
(Please use their login name i.e. <b>{{ user.get_username }}</b>)
</p>
<p><input required id="checkBox" type="checkbox" onclick="validate()"> I accept the terms and conditions</p>
<p id="text" style="display:none">You Have Agreed To the Terms and Conditions</p>
<input type="submit" value="Submit" />
</form>
</div>
</div>
</div>
{% endblock %}

Unable to get data from django choice form

Hi, I'm trying to develop a simple django app, and I'm having trouble accessing a form's data. I've looked at django's documentation extensively and many questions on here on the same topic but nothing is working. Here's my code inside my view, that is otherwise working:
def post(self, request):
"""Return only the games from the upcoming gameweek"""
form = GWForm(request.POST)
if form.is_valid():
curr_gw = form.cleaned_data['gweek']
args = {'form': form, 'this_gw_fixtures': Game.objects.filter(gweek=curr_gw), 'curr_gw': curr_gw}
return render(request, self.template_name, args)
else:
curr_gw = 17
form = GWForm()
args = {'form': form, 'this_gw_fixtures': Game.objects.filter(gweek=curr_gw), 'curr_gw': curr_gw}
return render(request, self.template_name, args)
And here's the code of my template:
<form action="/predict/" method="post">{% csrf_token %}
<label for="Gameweek">Gameweek: </label>
<input id="gwparam" type="number" value="{{ curr_gw }}" min="17" max="40">
<input type="submit" value="Go">
</form>
{% if this_gw_fixtures %}
<ul>
{% for game in this_gw_fixtures %}
<li>{{ game }}</li>
{% endfor %}
</ul>
{% else %}
<p>No game predictions are available for this gameweek.</p>
{% endif %}
What I'm trying to do is get the input of a choice form and render a list of games that are in the gameweek selected in the form. Minumum 17, max 40. Here's my form code.
class GWForm(forms.Form):
gweek = forms.ChoiceField(required = False, choices=[(x, x) for x in range(17, 40)])
But when I try to grab the gweek from the form, is_valid() returns true, but form.cleaned_data['gweek'] doesn't return any value at all. Any help would be appreciated. Thanks in advance.
forms.py
class GWForm(forms.Form):
gweek = forms.IntegerField(required=False, min_value=17, max_value=40)
template
<form action="/predict/" method="post">{% csrf_token %}
{{ form }}
<input type="submit" value="Go">
</form>

Django Admin Action using intermediate page

I have a model with a lot of fields. I only have a few fields I that I want to be required. So instead of the change list super long, I want to have a short change list then have admin actions that can give predefined subsets of the fields.
The initial action takes me to the correct page but when I submit the form it returns me to whatever page I designate, but doesn't update the fields. I am okay with tearing this down starting over again if needed. I think what I really need to know, what do I put in the action="" portion of the html to have the recursion work properly?
I am using django 1.7. I have to obfuscate a lot of my fields as a cya thing since I am working in a heavily information secure field.
Here is my admin.py
class CredentialAdmin(admin.ModelAdmin):
fields = ['reservedBy','reserveto']
list_display = ['reservedBy','reserveto']
class reserveToFormAdmin(forms.Form):
reservedBy = forms.CharField(widget=forms.Textarea, max_length=50)
reserveto = forms.DateTimeField(widget=forms.DateTimeInput)
def reserveCred(self, request, queryset):
form = None
plural = ''
if 'submit' in request.POST:
form = self.reserveToFormAdmin(request.POST)
for f in form.fields:
print f
print form.is_valid()
print form.errors
if form.is_valid():
reservetos = form.cleaned_data['reserveto']
reservedBys = form.cleaned_data['reservedBy']
print "hello"
count = 0
for cred in queryset:
cred.reserveto = reservetos
cred.reservedBy = reservedByss
cred.save()
count += 1
if count != 1:
plural = 's'
self.message_user(request, "Successfully reserved %s cred%s." % (count, plural))
return HttpResponseRedirect(request.get_full_path(),c)
if not form:
form = self.reserveToFormAdmin(initial={'_selected_action' : request.POST.getlist(admin.ACTION_CHECKBOX_NAME)})
return render(request,'admin/reserveCreds.html',{'creds':queryset, 'form':form, 'path':request.get_full_path()})
reserveCred.short_description = "Reserve Selected Creds"
actions = [check_out_a_cred,check_in_a_cred,audit_creds,CompareAudits,reserveCred]
reserveCreds.html
{% extends "admin/base_site.html" %}
{% block content %}
<p>How long and which department to reserver creds:</p>
<form action="{{ path }}" method="post">{% csrf_token %}
{{ form }}
<input type="submit" name="submit" value="submit" />
<input type="button" value = "Cancel" />
</form>
<h2> reserving: </h2>
<ul>
{% for cred in creds %}
<li> {{ cred.userid }} </li>
{% endfor %}
</ul>
{% endblock %}

Django modelformset_factory saves nothing

(I'm quite new at django, i googled for this a lot but could not find an answer, so i hope you don't mind the low level question)
There are these Classes:
class Cl(models.Model):
user = models.ForeignKey(User,editable=False)
title = models.CharField(max_length=100)
class Member(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
cl = models.ForeignKey(Cl)
class ClForm(ModelForm):
class Meta:
model = Cl
I want to make a page where i can create an Object of Cl and some related Member Objects (I plan to use django-dynamic-formset but that is not the point here). My view looks like this:
class NewView(TemplateView):
def get(self, request):
cform = ClForm(prefix="cl", instance=Cl(user=request.user))
MemberFormSet = modelformset_factory(Member)
memberfs = MemberFormSet(queryset=Member.objects.none())
return render_to_response(self.template_name, {'cl_form': cform, 'memberformset': memberfs}, context_instance=RequestContext(request))
def post(self,request):
cform = ClForm(request.POST, prefix="cl", instance=Cl(user=request.user))
MemberFormSet = modelformset_factory(Member)
memberfs = MemberFormSet(request.POST)
if cform.is_valid() and memberfs.is_valid():
new_cl = cform.save()
new_members = memberfs.save(commit=False)
for mf in new_members:
mf.cl = new_cl
mf.save()
return HttpResponseRedirect("unimportant")
return render_to_response(self.template_name, {'cl_form': cform, 'memberformset': memberfs}, context_instance=RequestContext(request))
My template looks like this:
<form id="myForm" method="post" action="">
{% csrf_token %}
{{ memberformset.management_form }}
<table>
{{ cl_form }}
<tr><th colspan="2">Members</th></tr>
{% with memberformset.empty_form as form %}
<div id="id_empty_form" style="display:none;">
{{ form }}
</div>
{% endwith %}
</table>
<input type="submit" value="Save" />
</form>
My problem is now, that the member is not stored. Also, the memberfs.is_valid() doens't complain, if e.g. the Email field is not filled out correctly.
I also tried the inlineformset_factory, but that didn't worked either.
I think the problem is that you are using the "empty_form" in that template. The correct way of using the formset is described here. In your example, you should write:
<form id="myForm" method="post" action="">
{% csrf_token %}
{{ memberformset.management_form }}
<table>
{{ cl_form }}
<tr><th colspan="2">Members</th></tr>
<!-- THIS IS NOT NECESSARY UNLESS YOU WANT TO ADD FORMS WITH JAVASCRIPT-->
<div id="id_empty_form" style="display:none;">
{{ memberformset.empty_form }}
</div>
<!-- END NOT NECESSARY-->
{% for form in memberformset.forms %}
{{ form }}
{% endfor %}
{% endwith %}
</tbody>
</table>
<input type="submit" value="Save" />
</form>