Can't advance Django paginator after formset submit - django

[** found a fix, see below **]
I'm having trouble getting Django 2 Paginator to work with a modelformset. There are three models, Place & Hit (one-to-many), and Link. The 'validator' view pages through Place objects 1 at a time, builds a queryset of Hits filtered by the FK placeid. The context sent to the template includes 1) the formset=HitFormSet, 2) a 'records' list with only the one Place object, and 3) the Paginator page.
The template renders the single Place record on the left side, and a scrolling list of Hit forms on the right. The Hit form has two added fields, 'match' (3 radio buttons) and 'flag' (checkbox). The user selects those if one or more Hits match the Place. Upon submitting, a new Link record is created with a placeid, a hitid, and values from the radios and checkbox. Also, a 'reviewed' field in the Place record is set to True.
The code below works to load Place #1, then page through the records - displaying a Place and its Hits. Clicking the Save button creates a new Link record as desired. The problem is that after the save, although the next Page loads on the left, its corresponding hits don't. By displaying pprint(locals()) I can see the correct hits are in the queryset, but the Hit fields in the formset all retain the values from the previous set of forms. The Paginator is advancing and the next Place loads, but not its Hit formset.
I've banged at this for a couple days, read docs, searched, etc. Any ideas?
view.py
def validator(request):
record_list = Place.objects.order_by('placeid').filter(reviewed=False)
paginator = Paginator(record_list, 1)
page = request.GET.get('page')
records = paginator.get_page(page)
count = len(record_list)
context = {
'records': records,
'page': page if request.method == 'GET' else str(int(page)-1)
}
placeid = records[0].placeid
hitid = records[0].hitid
q = Hit.objects.filter(placeid=placeid)
HitFormset = modelformset_factory(
Hit, fields = ['id','hitid', ],form=HitModelForm,extra=0)
formset = HitFormset(request.POST or None, queryset=q)
context['formset'] = formset
if request.method == 'GET':
method = request.method
print('a GET')
else:
if formset.is_valid():
print('formset is valid')
for x in range(len(formset)):
link = Link.objects.create(
placeid = placeid,
hitid = formset[x].cleaned_data['hitid'],
match = formset[x].cleaned_data['match'],
flag = formset[x].cleaned_data['flag'],
)
# flag Place record as reviewed
matchee = get_object_or_404(Place, placeid = placeid)
matchee.reviewed = True
matchee.save()
else:
print('formset is NOT valid')
print(formset.errors)
pprint(locals())
return render(request, 'validator/template.html', context=context)
template.html
{% block content %}
<div class="pagination">
<span class="step-links">
... all standard, works fine
</span>
</div>
{% for record in records %}
{% if records.has_next %}
<!-- <form id="form_related" method="POST" action="" > -->
<form id="form_related" method="POST" action="?page={{ records.next_page_number }}" >
{% else %}
<form id="form_related" method="POST" action="" >
{% endif %}
{% csrf_token %}
{{ formset.management_form }}
<input type="hidden" name="placeid" value="{{ record.placeid }}" />
{% for form in formset %}
<div class="row">
<div class="col-sm-4 id="place-record">
<!-- Place attributes -->
</div>
<div class="col-sm-8" id="hit-forms">
<div id="review">
<span>{{ form.match }} flag: {{ form.flag_geom }}</span>
</div>
<div id="hit">
<!-- Hit attributes -->
</div>
</div>
</div>
{% endfor %}
{% endfor %}
{% endblock%}

The answer (or an answer) turned out to be doing a redirect immediately after the save/POST. The save of Link record removes the Place from the queue, so the page is always "1"
view.py
...
if formset.is_valid():
for x in range(len(formset)):
link = Link.objects.create(
placeid = placeid,
tgnid = formset[x].cleaned_data['tgnid'],
match = formset[x].cleaned_data['match'],
flag_geom = formset[x].cleaned_data['flag_geom'],
)
matchee = get_object_or_404(Place, placeid = placeid)
matchee.reviewed = True
matchee.save()
return redirect('/formset/?page='+page)
else:
print('formset is NOT valid')
print(formset.errors)
...

Related

Flask form not getting validated

My flask form is not getting validated.
#admin_blueprints.route('/ManageMovies',methods=['GET', 'POST'])
def ManageMovie():
form = SetShowForm(request.form)
if request.method == 'POST' and form.validate():
print(form.movie.data)
return redirect(url_for('admin.AdminHome'))
engine = create_engine('mssql+pyodbc://DESKTOP-6UNRAN0/movie_f?driver=SQL Server?
Trusted_Connection=yes')
form.movie.choices = [(movie.m_id, movie.m_name)for movie in (engine.execute('select * from
MovieMaster'))]
form.show_time.choices = [(time.s_id, time.s_time) for time in (engine.execute('select * from
ShowTime'))]
return render_template('manage_movies.html',form=form)
my template code is
{% extends "master.html" %}
{% block content %}
<form method="POST">
{{ form.hidden_tag() }}
{{form.movie.label}}{{form.movie(class="form-control")}}
<br>
{{ form.show_time.label }} {{form.show_time(class="form-control")}}
<br>
{{form.price.label}} {{ form.price(class="form-control") }}
<br>
{{form.submit(class="btn btn-success")}}
</form>
{% endblock %}
my flask form
class SetShowForm(FlaskForm):
movie = SelectField('Movie Name', choices=[])
show_time = SelectField('Set Show Time',choices=[])
price = IntegerField('Price')
submit = SubmitField("Set")
Once I click on my submit button, the same page gets rendered again instead of entering my (if request.method == 'POST' and form.validate():) statement and printing the data. I have no idea what is going wrong. I am filling all the fields. Are there any rule for form validation.
I believe you need to use:
if form.validate_on_submit():
And you don't need to check for "POST" because validate_on_submit does that too.
Try it
#admin_blueprints.route('/ManageMovies',methods=['GET', 'POST'])
def ManageMovie():
form = SetShowForm()
if form.validate_on_submit():
print(form.movie.data)
return redirect(url_for('admin.AdminHome'))
return render_template('manage_movies.html',form=form)
And set choices values in setShowForm() directly

Implementing confirmation view and template in django

I have two views one that accepts inputs and the other for confirmation and execution of an action. My problem is how to confirm the inputs from another view and template.
This is similar when you delete a record. That you should confirm from the user his actions.
Here is the input view. PreprocessinputationView:
def PreprocessInputationView(request, **kwargs):
proj_pk = kwargs.get('pk')
project = Project.objects.get(id=proj_pk)
df = pd.read_csv(project.base_file)
n_cols = df.keys
context = {}
context['df'] = df
context['n_cols'] = n_cols
context['project'] = project
if request.method == 'POST':
# try:
checked_value = request.POST.getlist(u'predictors')
method = ''.join(request.POST.getlist(u'method'))
if checked_value and method:
context['checked_value'] = checked_value
context['method'] = method
return render(request, 'projects/preprocess/confirm_inputation.html', context)
return render(request, 'projects/preprocess/preprocess_inputation.html', context)
The confirmation view goes here. ConfirmInputationView:
def ConfirmInputationView(request, context):
print('method:', context['method'])
project = context['project']
df = pd.read_csv(project.base_file)
n_cols = df.keys
filename = project.base_file.name
tmp = filename.split('/')
filename = str(tmp[1:])
if request.method == 'POST':
# try:
checked_value = context['checked_value']
method = context['method']
if checked_value and (method=='mean'):
df[checked_value].fillna(df[checked_value].mean())
# df.drop(columns=checked_values, inplace=True)
new_df = df.to_csv(index=False)
updated_file = ContentFile(new_df)
updated_file.name = filename
project.base_file = updated_file
project.save()
str_checked_value = ', '.join(checked_value)
context['str_checked_value'] = str_checked_value
if str_checked_value:
messages.success(request, f'Inputation to column(s) {str_checked_value} successful!')
return render(request, 'projects/preprocess/preprocess_inputation.html', context)
The confirmation template. Confirm_inputation.html:
{% extends "base.html" %}
{% block page_heading %}
<div class="d-sm-flex align-items-center justify-content-between mb-4">
<h1 class="h3 mb-0 text-gray-800">Delete Project</h1>
</div>
{% endblock page_heading %}
{% block content %}
<div class="jumbotron col-xl-8 col-md-6 mb-1"">
<form method=" POST">
{% csrf_token %}
<fieldset class='form-group'>
<p>
You have chosen <strong>{{ method }}</strong> as an inputation method?
Are you sure you want to proceed?
</p>
</fieldset>
<div class="form-group">
<button class="btn btn-danger float-sm-right mr-1" type="submit">Yes, Delete</button>
<a class="btn btn-secondary float-sm-right mr-1" href="{% url 'project-detail' project.id %}">Cancel</a>
</div>
</form>
</div>
{% endblock content %}
The data from the PreprocessImputationView should be passed to ConfirmImputationView for confirmation and processing.
I'm not sure I understand your question or rather problem. So I'll summarize what I understood. Please clarify more, if this doesn't fit your problem.
You have view A (PreprocessInputationView), which shows the user some values/forms and allows some POST action to A.
If view A receives a POST request you check the form input and render template B of view B.
Your rendered template B offers two options to the user: Accept, which triggers POST to view B or decline, which links to some details view.
I think what you're missing is, that context in render is "lost" after rendering. The moment the user sees the finished html page that variable is no longer relevant and inaccessible.
A way to provide the necessary 'method' information to your B view would be to add a form field to your B template, which holds some sort of key for B view to determine on POST what to do. Like a hidden input field with a number. Each number would predetermined stand for a method.

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 pagination in filtered search post results

I have a view that filters out results for a posted search form:
def profile_advanced_search(request):
args = {}
if request.method == "POST":
form = AdvancedSearchForm(request.POST)
qs=[]
if form.is_valid():
cd = form.cleaned_data
s_country=cd['country']
s_province=cd['province']
s_city = cd['city']
if s_country: qs.append(Q(country__icontains = s_country))
if s_province: qs.append( Q(province__icontains=s_province))
if s_city: qs.append( Q(city__icontains=s_city))
f = None
for q in qs:
if f is None:
f=q
else: f &=q
list = UserProfile.objects.filter(f).order_by('-created_at')
else:
form = AdvancedSearchForm()
list = UserProfile.objects.all().order_by('-created_at')
paginator = Paginator(list,10)
page= request.GET.get('page')
try:
results = paginator.page(page)
except PageNotAnInteger:
results = paginator.page(1)
except EmptyPage:
results = paginator.page(paginator.num_pages)
args.update(csrf(request))
args['form'] = form
args['results'] = results
return render_to_response('userprofile/advanced_search.html', args,
context_instance=RequestContext(request))
the urls.py part is:
url(r'^search/$', 'userprofile.views.profile_advanced_search'),
The results in the first page are fine but the problem is that when I go to the second page, the filtered results is just forgotten.
It is obvious to me why this happnes: filtering in the views accounts only for POST while pagination uses GET hence the queryset filter does not apply after the first page.
I have looked at several suggestions and snippets for similar problem but none was close enough to my views, so I could not figure out how to fix it and appreciate your help.
Update: here is the relevant template:
<form action="/search/" method="post">{% csrf_token %}
<ul class="list-unstyled">
<li><h3>Country</h3></li>
<li>{{form.country}}</li><br>
<h4>Province</h4>
<li>{{form.province}}</li>
<h4>City</h4>
<li>{{form.city}}</li>
</ul>
<input type="submit" name="submit" value="search" />
</form>
Search Results:
{% for p in results %}
<div">
<div>
<br>
<strong><a href="/profile/{{p.username}}" >{{p.username}}</a></strong>
{{p.country}} <br>
{{p.province}} <br>
{{p.city}} <br>
</div>
</div>
{% endfor %}
<div>
<div class="pagination">
{% if results.has_previous %}
<< Prev &nbsp
{% endif %}
{% if results.has_next %}
Next >>
{% endif %}
</div>
</div>
</div>
You should move to Post/Redirect/Get pattern. You need 2 views, first one for form and second for results.
Create a new url able to parse all three parameters plus page. /(?P<country>\w+)/(?P<province>\w+)/(?P<site>\w+)/(?P<page>\d+). Route this url to a new view show_results. This view shows page results filtering for all three parameters.
In view form, after receive search parameters, compose url and make a redirect to it. return HttpResponseRedirect(reverse('your.views.show_results', args=(country, province, site, 0, )))
Obviously, instead to named url parameters you can work with GET key value parameters.
Take a look how google works in this way:
https://www.google.es/search?q=post+redirect+get

ChoiceField form is not valid and returns "too many values to unpack"

So, I am having this form where I display a list of all my app models in a drop-down list and expect from the user to choose one so as to display its fields. Below is my form and the models() method, which creates the list of models to be passed as argument to my ChoiceField.
*forms.py*
class dbForm(forms.Form):
model_classes_field = forms.ChoiceField(choices=models(), required=True,)
def models():
apps = get_app('Directories')
m_id = 0
for model in get_models(apps):
m_id += 1
model_classes.append({
'model_name': model._meta.verbose_name,
'model_id': m_id,
'model_table': model._meta.db_table,
'model_object': model.objects.all()
})
return model_classes
In my views.py I tried handling the POST data but unfortunately for some reason the form was not valid and I couldn't manipulate any data. Furthermore form.errors does not display anything at all.
*views.py*
def index(request):
if request.method == 'POST': # If the form has been submitted...
form = dbForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
model_classes_field = form.cleaned_data['model_classes_field']
return HttpResponseRedirect('/list/') # Redirect after POST
else:
print "form: ", form.errors
else:
form = dbForm() # An unbound form
print "form: ", form.errors
print "not valid form"
return render(request, 'Directories/index.html', {
'form': form,
})
Furthermore, in the template whenever i try to submit the form it returns an error message "too many values to unpack" and does not redirect me to the next template (list.html).
*index.html*
{% block content%}
<div id="content" align="center">
<h2> Welcome! this is Directories app! </h2>
<form action="" method="post">{% csrf_token %}
{{ form.model_classes_field.errors }}
<label for="id_model_classes_field">Choose from one of the existing tables:</label>
<select id="id_model_classes_field" name="model_classes_field">
{% for choice in form.fields.model_classes_field.choices %}
<option name="m_id" value="{{ choice.model_table }}">{{choice.model_id}}: {{choice.model_name}}</option>
{% endfor %}
</select> <br />
<input type="submit" value="Change" name="_change" />
</form>
</div>
<div id="bottom">
</div>
{% endblock %}
The only workaround i found on this is to fill the form action with the template to be redirected at (i.e. action = "list") instead of doing it in the views with return HttpResponseRedirect('/list/') . However, I believe that this does not solve the issue since still the form is not valid and i cannot process data with form.cleaned_data. It's peculiar though that the post data is sent even though the form is not valid.
*
EDIT: Solution
I changed my models() method as such:
def models():
apps = get_app('Directories')
for model in get_models(apps):
model_classes.append( (model._meta.verbose_name, model._meta.db_table), )
return model_classes
so I included a tuple as instructed by #Rohan and after making a slight alteration to my index.html:
...
{% for choice in form.fields.model_classes_field.choices %}
<option name="m_id" value="{{choice.0}}">{{choice.0}}</option>
{% endfor %}
...
form is valid and can now process my data.
*
Value of choices should be list/tuple containing items with exactly 2 elements. But you are creating list of dicts which might be causing the issue.
for model in get_models(apps):
m_id += 1
model_classes.append({
'model_name': model._meta.verbose_name,
'model_id': m_id,
'model_table': model._meta.db_table,
'model_object': model.objects.all()
})
So you may want to update models() method to return appropriate list/tuple.