Select a specific object from a queryset django - python

Is there any way to select a specific object that is found in the results of a queryset like selecting an item in an array.
In arrays, you can select a specific item based on its position:
arrayName = {55, 33, 34, 23}
arrayName[2]
result = 34
I want to accomplish the same thing with queryset results
Users in database = {Steve, Chris, Jame, Cole, Casper, Courtney}
Query will filter the names that start with c
result = {Chris, COle, Casper, Courtney}
After I get the results, I want to select Casper from the results...
is there a way to do it like an array.
something like results[2]
UPDATE
So I have the part of the view working that will specify a specific record.
My only other question is to see if it is possible to do the same thing in the HTML tempalte file... Here is what I have in the view and html file. is there a way to do the same thing ht ehtml file...
view.py
i=0
for form in formset:
cd = form.cleaned_data
currentAmount = cd['amount']
currentDescription = cd['description']
print(currentAmount)
print(currentDescription)
currentTrans = transactions[i]
currentTrans.amount = currentAmount
currentTrans.description = currentDescription
currentTrans.save()
print(currentTrans)
i = i + 1
html
<form action="." method="POST">
{% csrf_token %}
{{ SplitFormSet.management_form }}
{% for form in SplitFormSet %}
{{ form.as_p }}
{% endfor %}
<p>Tax: <input type="text" name="tax" value=""></p>
<p>Tip: <input type="text" name="tip" value=""></p>
<input type="submit" name="submit" value="submit">
</form>
I tried this but it game me an error becuase 'i' is not a tag
<form action="." method="POST">
{% csrf_token %}
{{ SplitFormSet.management_form }}
{% i = 0 %}
{% for form in SplitFormSet %}
{{ transactions[i] }}
{{ form.as_p }}
{% i = i + 1 %}
{% endfor %}
<p>Tax: <input type="text" name="tax" value=""></p>
<p>Tip: <input type="text" name="tip" value=""></p>
<input type="submit" name="submit" value="submit">
</form>

names=Model.objects.filter(name__istartswith='c')
print(names[2])
In place of name you have put the field name of you model

Related

how to search in mulit model using django

i want search from all my model in database , so i create this search functions for do this ... but always i got ' No results for this search '
how to fix this ?
view.py:
def search(request):
if request.method == 'GET':
query= request.GET.get('q')
submitbutton= request.GET.get('submit')
if query is not None:
home_database= Homepage.objects.filter(name__icontains=query,app_contect__icontains=query,page_url__icontains=query,app_image__icontains=query)
pcprograms_database= PCprogram.objects.filter(name__icontains=query,app_contect__icontains=query,page_url__icontains=query,app_image__icontains=query)
androidapk_database= AndroidApks.objects.filter(name__icontains=query,app_contect__icontains=query,page_url__icontains=query,app_image__icontains=query)
androidgames_database= AndroidGames.objects.filter(name__icontains=query,app_contect__icontains=query,page_url__icontains=query,app_image__icontains=query)
antiruvs_database= Antivirus.objects.filter(name__icontains=query,app_contect__icontains=query,page_url__icontains=query,app_image__icontains=query)
systems_database= OpratingSystems.objects.filter(name__icontains=query,app_contect__icontains=query,page_url__icontains=query,app_image__icontains=query)
pcgames_database= PCgames.objects.filter(name__icontains=query,app_contect__icontains=query,page_url__icontains=query,app_image__icontains=query)
results= list(chain(home_database,pcprograms_database,androidapk_database,androidgames_database,antiruvs_database,systems_database,pcgames_database))
context={'results': results,
'submitbutton': submitbutton}
return render(request, 'html_file/enterface.html', context)
else:
return render(request, 'html_file/enterface.html')
else:
return render(request, 'html_file/enterface.html')
html search form :
<form id="search_form" action="{% url 'search' %}" method="GET" value="{{request.GET.q}}">
<input id="search_box" type="text" name="q" value="{{request.GET.q}}"
placeholder=" Search For ... "/>
<input id="search_button" type="submit" name="submit" value="Search"/>
</form>
html file :
{% if submitbutton == 'Search' and request.GET.q != '' %}
{% if results %}
<h1> <small> Results for </small><b>{{ request.GET.q }}</b> : </h1>
<br/><br/>
{% for result in results %}
<label id="label_main_app"> <img id="img_main_app_first_screen" src="{{result.app_image.url}}" alt="no image found !" height="170" width="165" > {{result.name}} <br><br> <p id="p_size_first_page"> {{result.app_contect}} <br> <br> <big> See More & Download </big> </p>
</label>
{% endfor %}
{% else %}
<h3> No results for this search </h3>
{% endif %}
{% endif %}
any help please ?
If you are trying to filter with multiple model fields you need Q from django.db.models
Homepage.objects.filter(Q(name__icontains=query)&Q(app_contect__icontains=query),....

Flask submit on nested form

I have a multistep form like this:
{{ f.begin_form(form) }}
{% if step == "input_student_email" %}
<form action="{{ url_for('operator.add_schedule') }}" method="get">
{{ f.render_field(form.student_email) }}
<input type=hidden name="step" value="available_course">
<input type="submit" value="Check available course">
</form>
{% elif step == "available_course" %}
<form action="{{ url_for('operator.add_schedule') }}" method="get">
{{ f.render_field(form.course_name) }}
<input type=hidden name="step" value="type_of_class">
<input type="submit" value="Check type of class">
</form>
{% elif step == "type_of_class" %}
<form action="{{ url_for('operator.add_schedule') }}" method="get">
{{ f.render_field(form.type_of_class) }}
<input type=hidden name="step" value="input_schedule">
<input type="submit" value="Input Schedule">
</form>
{% elif step == "input_schedule" %}
<form action="{{ url_for('operator.add_schedule') }}" method="get">
{{ f.render_field(form.schedule_month) }}
{{ f.render_field(form.schedule_day) }}
{{ f.render_field(form.start_at) }}
{{ f.render_field(form.end_at) }}
<input type=hidden name="step" value="input_teacher_email">
<input type="submit" value="Check Teacher">
</form>
{% elif step == "input_teacher_email" %}
<form action="{{ url_for('operator.add_schedule') }}" method="get">
{{ f.render_field(form.teacher_email) }}
<input type="submit" value="Submit">
</form>
{% endif %}
{{ f.end_form() }}
and here is the route:
#operator.route('/add-schedule', methods=['GET', 'POST'])
def add_schedule():
form = ScheduleForm()
if "step" not in request.form:
return render_template('manipulate-schedule.html', form=form, step="input_student_email")
elif request.form["step"] == "available_course":
session['student_email'] = form.student_email.data
return render_template('manipulate-schedule.html', form=form, step="available_course")
elif request.form["step"] == "type_of_class":
session['course_name'] = str(form.course_name.data)
return render_template('manipulate-schedule.html', form=form, step="type_of_class")
elif request.form["step"] == "input_schedule":
session['type_of_class'] = form.type_of_class.data
return render_template('manipulate-schedule.html', form=form, step="input_schedule")
elif request.form["step"] == "input_teacher_email":
if form.validate_on_submit():
student_email = str(session.get('student_email'))
course_name = str(session.get('course_name'))
type_of_class = str(session.get('type_of_class'))
schedule_month = form.schedule_month.data
schedule_day = form.schedule_day.data
start_at = str(form.start_at.data)
end_at = str(form.end_at.data)
teacher_email = form.teacher_email.data
student = db.session.query(Student).filter_by(email=student_email).first()
course = db.session.query(Course).filter_by(name=course_name).first()
teacher = Teacher.query.filter_by(email=teacher_email).first()
# test the result
test_all_data = str(student_email) + str(course_name) + str(type_of_class) + str(schedule_month) + str(
schedule_day) + str(start_at) + str(end_at)
print('test', test_all_data)
schedule = Schedule(
student_id=student.id,
course_id=course.id,
type_of_class=type_of_class,
schedule_month=schedule_month,
schedule_day=schedule_day,
start_at=start_at,
end_at=end_at,
teacher_id=teacher.id
)
db.session.add(schedule)
try:
db.session.commit()
except Exception as e:
db.session.rollback()
return redirect(url_for('operator.all_schedules'))
return render_template('manipulate-schedule.html', form=form, step="input_teacher_email")
return render_template('manipulate-schedule.html', form=form)
When I try to insert the data directly without nested form, it works perfectly.
But whenever I try to use a nested form like above, the URL just redirects without inserted to the database. The thing that makes me confuse is, it just redirect without any error message.
So, the point of my question is, is it posible to insert data to the database if we using nested form..?
Generally, forms could not be nested - Can you nest html forms?
Specifically, did you look at the data that are being POSTed by the form?
To avoid nesting forms in this case, you can wrap all subforms in the one <form> tag without any need to have multiple form tags or even nesting them.

iterate trough a forms in dictionary passed from view to html in python with jinja

I would appreciate your help in the below problem:
I need to use 3 different models in a python app which I want the user to populate from a HTML form. I want to use model forms and I pass all the three different form in one dictionary (see my views.py):
def new_sales_trip(request):
if request.method == "POST":
Restaurant_Form = RestaurantForm(request.POST)
SalesEvent_Form = SalesEventForm(request.POST)
NextAction_Form = NextActionForm(request.POST)
if Restaurant_Form.is_valid() and SalesEvent_Form.is_valid() and NextAction_Form.is_valid():
Restaurants = Restaurant_Form.save()
SalesEvents = SalesEvent_Form.save(False)
NextActions = NextAction_Form.save(False)
SalesEvents.restaurants = Restaurants
SalesEvents.save()
NextActions.restaurants = Restaurants
NextActions.save()
return redirect('/')
else:
allforms = {
'Restaurant_Form': Restaurant_Form,
'SalesEvent_Form': SalesEvent_Form,
'NextAction_Form': NextAction_Form,
}
return render(request, 'sales/SalesTrip_form.html', allforms)
else:
Restaurant_Form = RestaurantForm()
SalesEvent_Form = SalesEventForm()
NextAction_Form = NextActionForm()
c = {}
c.update(csrf(request))
allforms = {
'Restaurant_Form': Restaurant_Form,
'SalesEvent_Form': SalesEvent_Form,
'NextAction_Form': NextAction_Form,
}
return render(request, 'sales/SalesTrip_form.html', allforms)
So far it works - however I don't know how to use this dictionary to iterate trough in my template so I shouldn't reference all the form by name separatelly. I try to to do something like this:
{% for key, value in allforms.items %}
{% for field in value %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10"> <!--this gonna be only displayed if there is an error in it-->
<span class="text-danger small">{{ field.errors }}</span>
</div>
<label class="control-label col-sm-2">{{ field.label_tag }}</label>
<div class="col-sm-10"> <!--this gonna be displayed if there is no error -->
{{ field }}
</div>
</div>
{% endfor %}
{% endfor %}
Unforunatelly it is not rendering anything but a blank page. If I try the .items() property than it says:
Could not parse the remainder: '()' from 'allforms.items()'
You just need to add () to the end of items:
e.g.,
{% for key, value in allforms.items() %}
{% for field in value %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10"> <!--this gonna be only displayed if there is an error in it-->
<span class="text-danger small">{{ field.errors }}</span>
</div>
<label class="control-label col-sm-2">{{ field.label_tag }}</label>
<div class="col-sm-10"> <!--this gonna be displayed if there is no error -->
{{ field }}
</div>
</div>
{% endfor %}
{% endfor %}

'function' object has no attribute 'objects' Django 1.8 searching a model

I'm getting this error:
Exception Type: AttributeError
Exception Value:
'function' object has no attribute 'objects'
web/views.py in consultarE, line 153
from this:
views.py
def consultarE(request):
query = request.GET.get('q', '')
if query:
qset = (
Q(nombres__icontains=query) |
Q(apellidos__icontains=query) |
Q(telefono__icontains=query)
# Q(cargo__icontains=query)
)
results = Empleado.objects.filter(qset).distinct()
else:
results = []
return render_to_response("web/consultarEmpleado.html", {
"results": results,
"query": query
})
urls.py
url(r'^consultarEmpleado/$','consultarE',name='consultarEmpleado'),
consultarEmpleado.html
{% block content%}
<h1>Ingrese su búsqueda</h1>
<form class="navbar-form" role="search" action="." method="GET">
{{form|crispy}}
<label for="q">Buscar: </label>
<div class="input-group">
<input type="text" class="form-control" placeholder="Search" name="q" value="{{ query|escape }}">
<div class="input-group-btn">
<button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
{% if query %}
<h2>Results for "{{ query|escape }}":</h2>
{% if results %}
<ul>
{% for empleado in results %}
<li>{{ empleado|escape }}</l1>
{% endfor %}
</ul>
{% else %}
<p>No se encontró empleado</p>
{% endif %}
{% endif %}
{%endblock%}
Check if there's a function called Empleado which overwrite reference to Empleado model.
def Empleado(...):
....
Adjust the function name not to conflict with the model name.

Python Django, submit multiple forms

i am fairly new to Django and Python and have the following problem:
I have a quiz with multiple answers.
Every answer has a form attached to it, which posts data to a view that creates an objects from a Model.
But i cant post all forms, because its only posting the 5th one.
views.py:
def quiz(request, quiz_id=1):
cs = {}
cs.update(csrf(request))
height=75*Frage.objects.count()
height_menu = height + 10
if Frage.objects.count() == 0:
height = 170
height_menu = height
len_quiz = len(Quiz.objects.get(id=quiz_id).title)
count=Frage.objects.filter(quiz=quiz_id).count
count_aw = 0
aw = Frage.objects.filter(quiz=quiz_id)
cs.update({'quiz': Quiz.objects.get(id=quiz_id),
'Frage': Frage.objects.filter(quiz=quiz_id),
'len': len_quiz,
'hg': height,
'hg_m': height_menu,
'user': request.user,
'aw': Antwort.objects.all(),
'count': count,
'count_aw': count_aw})
return render_to_response('quiz.html', cs)
def check_view(request):
check = request.POST.get('aw_check', '')
user_id = request.POST.get('user_log', '')
user_act = User.objects.get(id=user_id)
frage_id = request.POST.get('frage', '')
frage = Frage.objects.get(id=frage_id)
antwort_id = request.POST.get('antwort', '')
antwort = Antwort.objects.get(id=antwort_id)
richtig = request.POST.get('richtig', '')
quiz_id = request.POST.get('quiz', '')
quiz = Quiz.objects.get(id=quiz_id)
res = Results(quiz=quiz, frage=frage, user=user_act, richtig=richtig, choice=check, aw=antwort)
res.save()
return HttpResponseRedirect('/quiz/all/')
template:
{% if Frage.count == 0 %}
<br>
<h1 id="main_link" style="text-align: center" align="center">Keine Fragen vorhanden!</h1>
{% endif %}
<ol>
{% for Frage in Frage %}
<li><div id="frage">{{Frage.content}}</div></li>
{% for aw in Frage.antworten.all %}
<form action="/quiz/check/" name="{{ aw.content }}" method="post">{% csrf_token %}
<label for="aw_check">{{ aw.content }}</label>
<input type="checkbox" name="aw_check" id="aw_check">
<input type="hidden" name="user_log" value="{{ user.id }}">
<input type="hidden" name="frage" value="{{ Frage.id }}">
<input type="hidden" name="antwort" value="{{ aw.id }}">
<input type="hidden" name="quiz" value="{{ quiz.id }}">
{% if aw.richtig %}
<input type="hidden" name="richtig" value=True>
{% else %}
<input type="hidden" name="richtig" value=False>
{% endif %}
<input type="submit" value="Abgeben" />
<br>
</form>
{% endfor %}
{% endfor %}
</ol>
<script language="javascript">
function call_submitforms(){
{% for Frage in Frage %}
{% for aw in Frage.antworten.all %}
setTimeout("document.{{ aw.content }}.submit()",1000);
{{ check.it }}
{% endfor %}
{% endfor %}
}
</script>
<input type="button" value="Abschicken" onclick="call_submitforms()" />
I think you should redesign your program. My understanding is that form submission can only handle one form. Here are some work-arounds using Django/Python. But, for your case, if you're designing a quiz, all of the answers should be part of a single form. Instead of returning {% for Frage in Frage %} I would do something like this (you'll have to redesign your models and views first):
//one form contains all questions
form action="/quiz/check/" name="{{ aw.content }}" method="post">{% csrf_token %}
//list the questions as part of that form
{%for Frage in quiz.Frages%}
<input type="hidden" name="user_log" value="{{ user.id }}">
<input type="hidden" name="antwort" value="{{ aw.id }}">
<input type="hidden" name="quiz" value="{{ frage.id }}">
{% endfor %}
//submit all of the data for each page of the quiz in one form
<input type="hidden" name="richtig" value=True>
{% else %}
<input type="hidden" name="richtig" value=False>
{% endif %}
<input type="submit" value="Abgeben" />

Categories