Getting user by primary key in flask - python

I'm working with flask and have a html page that contains of user name(user.html) which took from table, Now How can I see more detail by clicking on each of users(which route to profile)?
I don't use login for app So I don't want to use g
app.py
# am I doing it right?
#app.route('/profile/<int:id>')
def profile(id=None):
detail = Contacts.query.get(id)
return render_template('profile.html', detail= detail , id=id)
user.html
{% extends "layout.html" %}
{% block content %}
<h2>show user</h2>
{% for contact in contact %}
# I got error when I click on each user name to see their 'profile'
#I guess because of id How can Solve it?
#error BuildError: ('profile', {}, None)
<strong>name:</strong><a href={{url_for('profile')}}>
{{ contact.name}}</a><br>
{% endfor %}
{% endblock %}
profile.html
{% extends "layout.html" %}
{% block content %}
<h2>show user profile</h2>
# how can I make it specific for each row of table(each user)?
{% for detail in Contacts %}
<strong>name:</strong> {{ detail.name}} <br>
<strong>email:</strong> {{ detail.email }} <br>
<strong>age:</strong> {{ detail.age}} <br>
<br>
{% endfor %}
{% endblock %}
model.py
class Contacts(db.Model):
__tablename__ = "Contacts"
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(50))
email = db.Column(db.String(50))
age = db.Column(db.Integer)
submit = SubmitField("Submit")

I noticed two things in your code:
# this
<a href={{url_for('profile')}}>
# should be
<a href={{url_for('profile', id=contact.id)}}>
# otherwise Flask can't find the route, because it needs an id
And the other one:
{% for detail in Contacts %}
There is no such thing as a Contacts variable in your template, because your view function does not send it. Just get rid of the loop and use detail directly, because it's what you sent to the template.

Related

Dynamically change WTForms field type between SelectField and HiddenField

I have a WTForms field (value_currency) that I want to sometimes be a SelectField and sometimes a HiddenField. I use the same view and template for a page that both creates new items and edits existing items. If I load the page to create a new item, I want this field to be a SelectField, and if I load the page to edit an existing item, I want this field to be a HiddenField because it's a non-editable field.
Here is what I have so far:
FORM
class PromoForm(Form):
value = StringField('value')
currencies = Currency.query.order_by(Currency.id).all()
currency_choices = []
for currency in currencies:
currency_choice = (currency.id, currency.name)
currency_choices.append(currency_choice)
value_currency = SelectField('value_currency', choices=currency_choices)
VIEW
#app.route('/promo/<id>', methods=['GET', 'POST'])
#login_required
def promo(id):
form = PromoForm()
# Existing promo will pass in its id
# id = 0 if a new promo is to be created
if id != str(0):
# Load existing promo
promo = Promo.query.get(id)
# display value in decimal format
form.value.default = "{0}.{1:0>2}".format(
promo.value_cents//100, promo.value_cents%100)
form.process()
return render_template('promo.html', promo=promo, form=form)
else:
# New promo
audit_log('GET', client, session=session)
return render_template('promo.html', form=form)
TEMPLATE
{% extends "base.html" %}
{% block content %}
{% if promo is defined %}
<form action="{{ url_for('.promo', id=promo.id) }}" method="post">
{% else %}
<form action="{{ url_for('.promo', id=0) }}" method="post">
{% endif %}
{{ form.hidden_tag() }}
<div>
<label for="value">Promo Value</label>
{% if promo is defined %}
{{ form.value() }}
{% else %}
{{ form.value() }}
{% endif %}
{% for error in form.value.errors %}
<span class="error">[{{ error }}]</span>
{% endfor %}
{% if promo is defined %}
# ----> Promo.value_currency should be a hidden field here (Doesn't work)
{{ promo.value_currency }}
{% else %}
# ----> Promo.value_currency is a select field here (Currently works)
{{ form.value_currency() }}
{% endif %}
</div>
<div class="submit_btn">
{% if promo is defined %}
<input type="submit" value="Update Promo">
{% else %}
<input type="submit" value="Create Promo">
{% endif %}
</div>
{% endblock %}
I know I can just simply hardcode the hidden input element and drop in the value with Jinja, but I prefer to do it with WTForms and not do any form element hard coding. Is that possible?
See (duplicated) question: Flask, WTForms: Is there a way to make a StringField in the form _temporarily_ hidden?.
You cannot just omit the field, and you cannot change its object type (from SelectField to HiddenField) either.
However, you can change its widget object dynamically.
Replace this with a HiddenInput.
from wtforms.widgets import HiddenInput
class PromoForm(Form):
value = StringField('value')
currencies = Currency.query.order_by(Currency.id).all()
currency_choices = []
for currency in currencies:
currency_choice = (currency.id, currency.name)
currency_choices.append(currency_choice)
value_currency = SelectField('value_currency', choices=currency_choices)
def hide_value_currency(self, value):
"""
Hide the value_currency field by morping it into a
HiddenInput.
"""
self.value_currency.widget = HiddenInput()
# wtforms chokes if the data attribute is not present
self.value_currency.data = value
# wtforms chokes on SelectField with HiddenInput widget
# if there is no _data() callable
self.value_currency._value = lambda: value
Call form.hide_value_currency(pre_set_value) in your view when required.
No logic in your template necessary.

TypeError at /careers/test/ jobpost_detail() got an unexpected keyword argument 'slug'

I installed the app careers mezanine and after creating a test position and turn the page to see the error TypeError at / careers / test /
jobpost_detail () got an unexpected keyword argument 'slug'. How do I fix this problem?
views
from calendar import month_name
from django.shortcuts import get_object_or_404
from collections import defaultdict
from django.contrib.contenttypes.models import ContentType
from django import VERSION
from careers.models import JobPost
from mezzanine.conf import settings
from mezzanine.generic.models import AssignedKeyword, Keyword
from mezzanine.utils.views import render, paginate
def jobpost_list(request, tag=None, year=None, month=None, template="careers/jobpost_list.html"):
"""
Display a list of job posts that are filtered by year, month.
"""
settings.use_editable()
templates = []
jobposts = JobPost.objects.published()
if tag is not None:
tag = get_object_or_404(Keyword, slug=tag)
jobposts = jobposts.filter(keywords__in=tag.assignments.all())
if year is not None:
jobposts = jobposts.filter(publish_date__year=year)
if month is not None:
jobposts = jobposts.filter(publish_date__month=month)
month = month_name[int(month)]
# We want to iterate keywords and categories for each blog post
# without triggering "num posts x 2" queries.
#
# For Django 1.3 we create dicts mapping blog post IDs to lists of
# categories and keywords, and assign these to attributes on each
# blog post. The Blog model then uses accessor methods to retrieve
# these attributes when assigned, which will fall back to the real
# related managers for Django 1.4 and higher, which will already
# have their data retrieved via prefetch_related.
jobposts = jobposts.select_related("user")
if VERSION >= (1, 4):
jobposts = jobposts.prefetch_related("keywords__keyword")
else:
if jobposts:
ids = ",".join([str(p.id) for p in jobposts])
keywords = defaultdict(list)
jobpost_type = ContentType.objects.get(app_label="careers", model="jobpost")
assigned = AssignedKeyword.objects.filter(jobpost__in=jobposts, content_type=jobpost_type).select_related("keyword")
for a in assigned:
keywords[a.object_pk].append(a.keyword)
for i, post in enumerate(jobposts):
setattr(jobposts[i], "_keywords", keywords[post.id])
jobposts = paginate(jobposts, request.GET.get("page", 1),
settings.CAREERS_PER_PAGE,
settings.MAX_PAGING_LINKS)
context = {"jobposts": jobposts, "year": year, "month": month, "tag": tag}
templates.append(template)
return render(request, templates, context)
def jobpost_detail(request, template="careers/jobpost_detail.html"):
""". Custom templates are checked for using the name
``careers/jobpost_detail_XXX.html`` where ``XXX`` is the job
posts's slug.
"""
jobposts = JobPost.objects.published()
jobpost = get_object_or_404(jobposts)
context = {"jobpost": jobpost, "editable_obj": jobpost}
templates = [u"careers/jobpost_detail_%s.html" %(slug), template]
return render(request, templates, context)
html
{% extends "careers/jobpost_list.html" %}
{% load mezzanine_tags keyword_tags i18n %}
{% block meta_title %}{{ jobpost.meta_title }}{% endblock %}
{% block meta_keywords %}{% metablock %}
{% keywords_for jobpost as tags %}
{% for tag in tags %}{% if not forloop.first %}, {% endif %}{{ tag }}{% endfor %}
{% endmetablock %}{% endblock %}
{% block meta_description %}{% metablock %}
{{ jobpost.description }}
{% endmetablock %}{% endblock %}
{% block title %}
{% editable jobpost.title %}{{ jobpost.title }}{% endeditable %}
{% endblock %}
{% block breadcrumb_menu %}
{{ block.super }}
<li class="active">{{ jobpost.title }}</li>
{% endblock %}
{% block main %}
<h6>
{% trans "Posted" %} {{ jobpost.publish_date|timesince }} {% trans "ago" %}.
</h6>
{% editable jobpost.content %}
{{ jobpost.content|richtext_filter|safe }}
{% endeditable %}
{% keywords_for jobpost as tags %}
{% if tags %}
{% spaceless %}
<ul class="unstyled tags">
<li>{% trans "Tags" %}:</li>
{% for tag in tags %}
<li>{{ tag }}</li>
{% endfor %}
</ul>
{% endspaceless %}
{% endif %}
{% set_short_url_for jobpost %}
<a class="btn small primary share-twitter" target="_blank" href="http://twitter.com/home?status={{ jobpost.short_url|urlencode }}%20{{ jobpost.title|urlencode }}">{% trans "Share on Twitter" %}</a>
<a class="btn small primary share-facebook" target="_blank" href="http://facebook.com/sharer.php?u={{ request.build_absolute_uri }}&t={{ jobpost.title|urlencode }}">{% trans "Share on Facebook" %}</a>
{% endblock %}
url
from django.conf.urls import patterns, url
# Job Post patterns.
urlpatterns = patterns("careers.views",
url("^tag/(?P<tag>.*)/$",
"jobpost_list",
name="jobpost_list_tag"),
url("^archive/(?P<year>\d{4})/(?P<month>\d{1,2})/$",
"jobpost_list",
name="jobpost_list_month"),
url("^archive/(?P<year>.*)/$",
"jobpost_list",
name="jobpost_list_year"),
url("^(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>.*)/$",
"jobpost_detail",
name="jobpost_detail_date"),
url("^(?P<slug>.*)/$",
"jobpost_detail",
name="jobpost_detail"),
url("^$",
"jobpost_list",
name="jobpost_list"),
)
The error tells you exactly what is going on: your "jobpost_detail" URL the captures a slug parameter and passes it on to the view, but that view does not expect a slug, only the request and a template. Also, you are not doing anything in that view to get the actual post identified by a slug: you are always getting the first published post.
I suspect you want to do the following:
def jobpost_detail(request, slug, template="careers/jobpost_detail.html"):
jobposts = JobPost.objects.published()
jobpost = get_object_or_404(jobposts, slug=slug)

In django how to access 2 field values in custom django modeladmin...includes/fieldset.html?

Edited my code: In the custom fieldset of a model admin:
{%load app_extras %}
{% if field.field.name == 'mobile' %}
<a target="hiddenIframe" href="http://url_to_call.php?exten={{request.user.employee_profile.extension}}&phone={{ field.field.value }}">Click-to-call</a>
{% my_mobile mobile=field.field.value as mob %}
{% endif %}
{% if field.field.name == 'sms_message' %}{{ mob }}
<a target="hiddenIframe" href="http://url_for_send_sms.php?sms_message={{ field.field.value }}&phone={{ mob }}">Click-to-send-sms</a>
{% endif %}
Here I am trying to access mobile number as well as sms_message fields of the model admin form simultaneously.
I have figured that I need to use custom tags, so I created the templatetags module, with app_extras.py containiging the function to assign the value of mobile and return it as follows:
#register.assignment_tag
def my_mobile(*args, **kwargs):
m_mobile = int(kwargs['mobile'])
return {'m_mobile': m_mobile }
In the template fiedset.html above note changes: This returns a Long value as: {'m_mobile': 1234534519L}
When seen on the browser for url for hyperlink shows:
http://url_for_send_sms.php/?sms_message=fgdfg&phone={%27m_mobile%27:%1234534519L}
How do I access the mobile number? Is my custom tag correct?
I formatted the output in my tag as:
#register.assignment_tag
def my_mobile(*args, **kwargs):
m_mobile = ("%d" %int(kwargs['mobile']))
return {'m_mobile': m_mobile }
In the template fieldset.html changed the code as:
{% if field.field.name == 'sms_message' %}
<a target="hiddenIframe" href="http://url_for_send_sms.php?sms_message={{ field.field.value }}&phone=={{ mob.m_mobile }}">Click-to-send-sms</a>
{% endif %}
Important: Both the mobile number and the sms_message are in the same line of the fieldset in the django modeladmin (in my case). So above code belongs to the loop {% for line in fieldset %} loop
Try
{% for ln in fieldset %}
{% for fld in ln %}
{% if f.field.name == 'mobile' %}
{{ f.field.value }}
{% endif %}
{% endfor %}
{% endfor %}
Maybe this is not the best solution ... but it is solution :)

Accessing dictionary values in django templates

Please help me in displaying the values of dictionary in django templates. I tried google to find out, but could not get the solution.
Below is the Model
class Ride(models.Model):
type = models.BooleanField(default=False)
add_source = models.ForeignKey(Address, related_name='source')
add_destination = models.ForeignKey(Address, related_name='destination')
ride_comment = models.TextField(null=True,max_length=140,blank=True)
def __unicode__(self):
return self.ride_comment
class Driver(models.Model):
ride_id = models.ForeignKey(Ride)
user_id = models.ForeignKey(User)
drv_carseats = models.SmallIntegerField(null=True,blank=False)
def __unicode__(self):
return self.user_id.username
View
for ride in result_list:
if ride.type:
driver = Driver.objects.get(ride_id = ride)
userList[ride.pk] = driver.user_id.username
print 'driver', driver.user_id.username, userList[ride.pk]
return render_to_response('rides/search.html', {'result_list':result_list,'userList':userList}, context )
And here is my template code
{% for result in result_list %}
{% if result %}
{{ userList[result.pk] }}
<em>{{ result.add_source }}</em>
<em>{{ result.add_destination }}</em>
<em>{{ result.ride_comment }}</em>
{% endif %}
{% endfor %}
I am getting the following error
TemplateSyntaxError at /rides/search/
Could not parse the remainder: '[result.pk]' from 'userList[result.pk]'
you should write a django custom filter for this.
create a file name get_dict_val.py inside your app..
project
-app
-templatetags
__init__.py
get_dict_val.py
Now in get_dict_val.py
#register.filter
def get_item(dictionary, key):
return dictionary.get(key)
In template
add this as first line write..
{% load get_dict_val %}
now replace in your code in template
{{ userList|get_item:result.pk }}
You don't need to create dictionary to access drivers at the template level, you can follow the relationship backward as Driver model has the foreign key for Ride model:
{% for result in result_list %}
{% if result %}
{% with result.driver_set.all as drivers %}
{% for driver in drivers %}
{{ driver.user_id }}
{% endfor %}
{% endwith %}
<em>{{ result.add_source }}</em>
<em>{{ result.add_destination }}</em>
<em>{{ result.ride_comment }}</em>
{% endif %}
{% endfor %}
It is good to specify the related_name for ForeignKey as it makes life easier to access objects:
ride_id = models.ForeignKey(Ride, related_name='drivers')
Then you can do:
ride = Ride.objects.get(id='some_id')
drivers = ride.drivers.all()

reacting to users posts

Regardless of my specific problem below, whats an effective way to respond multiple times to a users posts on the same page. So that on each progressive post on the page can capture new requests and display new information? Forgive me if i'm not describing the problem correctly.
I'm trying to build a page that reacts the the users posts. But i'm running into a :
Bad Request
The browser (or proxy) sent a request that this server could not
understand.
I'm guessing because in my current solution:
#app.route('/survey', methods = ['GET', 'POST'])$
#contributer_permission.require(403)$
def survey():
organization_id = None
survey_header_id = None
survey_section_id = None
organization_selected = None
survey_header_selected = None
survey_section_selected = None
if request.method== 'POST':
if not organization_id:
organization_id = request.form['organization_id']
organization_selected = Organization.query.get(organization_id)
elif not survey_header_id:
survey_header_id = request.form['survey_header_id']
survey_header_selected = SurveyHeader.query.get(survey_header_id)
elif not survey_section_id:
pass
else:
pass
return render_template('survey.html',
organization_class = Organization,
organization_selected = organization_selected,
organization_id = organization_id,
survey_header_id = survey_header_id,
survey_header_selected = survey_header_selected,
survey_section_id = survey_section_id,
survey_section_selected = survey_section_selected)
once i receive the post carrying the survey_header_id. It reloops and
organization_id becomes none
Here is the the accompanying html/json
{% extends "base.html" %}
{% block content %}
<div class ="entries"> <!-- should be a DIV in your style! -->
<form action="{{url_for('survey') }}" method="post" class="add-entry"/>
<dl>
{% if not organization_id %}
{% for organization in organization_class.query.all() %}
<dt><input type="radio", name="organization_id",
value="{{ organization.id }}"/>{{ organization.name }}</dt>
{% endfor %}
<dt><input type ="submit", name="submit_organization"/>
{% elif not survey_header_id %}
<h1>{{ organization_selected.name }}</h1>
{% for survey_header in organization_selected.survey_headers.all() %}
<dt><input type="radio", name="survey_header_id"
value="{{ survey_header.id }}"/>{{ survey_header.name }}
{% endfor %}
<dt><input type ="submit", name="submit_survey_header"/>
{% elif not survey_section_id %}
<p>hi</p>
{% else %}
{% endif %}
<dl>
</form>
</div>
{% endblock %}
What should i be doing?
Bad Request is generally the result of accessing a parameter that doesn't exist, such as request.form['organization_id'] when there is no element in the form with that name.
Your survey route will always attempt to retrieve organization_id from the form because you set it to None, and there's nothing to change that before it's tested. On the second Post, your template does not create an organization_id element at all because the value was passed from the previous Post, so you see an error when survey() still attempts to retrieve it.
You need some way to pass your submitted values from one step to the next. For example, you could write them to hidden or disabled fields within the form so you can grab them and send them back out to the template after each Post, or store your state somewhere else like the session.

Categories