how to show all post with relatedid in django? - python

hey guys i have trouble in display all post with same relatedid and related id comes from qna and i want to show all relatedid post to qna_post.html please help to fix it in django 1.11 python3
urls.py
from django.conf.urls import url, include
from django.views.generic import ListView, DetailView
from qna.models import Post
urlpatterns = [
url(r'^$', ListView.as_view(queryset=Post.objects.all().order_by("-date")[:25], template_name="qna.html")),
url(r'^(?P<pk>\d+)$', DetailView.as_view(model = Post, template_name= 'qna_post.html'))
]
qna_post.html
{% extends "header.html" %}
{% block content %}
{% for post in object_list %}
{% url page_item item.id %}
{% if post.relatedid == post.id %}
{% if post.PostType == 'q' %}
<h3>{{ post.Title }}</h3>
<h6>on {{ post.date }}</h6>
{{post.Body|safe|linebreaks}}
{% endif %}
{% if post.PostType == 'a' %}
<h3>{{ post.Title }}</h3>
<h6>on {{ post.date }}</h6>
{{post.Body|safe|linebreaks}}
{% endif %}
{% endif %}
{% endfor %}
{% endblock %}
qna.html
{% extends "header.html" %}
{% block content %}
{% for post in object_list %}
{% if post.PostType == 'q' %}
<h5>{{ post.date|date:"Y-m-d" }} {{ post.Title }} -<label> {{ post.Body|slice:":8" }}...</label></h5>
{% endif %}
{% endfor %}
{% endblock %}

Related

Django : how to divide result from search

Assume I've created a search views with chain (multiple search from different app).
I want to display my result at the same place:
{% for object in object_list %}
{% with object|class_name as klass %}
{% if klass == 'Post' %}
{{ object.title }}
{% elif klass == 'Catego' %}
{{ object.name }}
{% elif klass == 'UserProfile' %}
{{ object.user.username }}
{% else %}
{% endif %}
{% endwith %}
{% empty %}
{% endfor %}
Everything is ok. I got all of my result.
Now, if I want to separate this result in different div (row and collapse class).
If I try this:
<div class="row collapse" id="collapseNutri">
{% for object in object_list %}
{% with object|class_name as klass %}
{% if klass == 'Post' %}
{{ object.title }}
{% endif %}
{% endwith %}
{% endfor %}
</div>
<div class="row collapse" id="collapseCatego">
{% for object in object_list %}
{% with object|class_name as klass %}
{% if klass == 'Catego' %}
{{ object.name }}
{% endif %}
{% endwith %}
{% endfor %}
</div>
<div class="row collapse" id="collapseUserProfile">
{% for object in object_list %}
{% with object|class_name as klass %}
{% if klass == 'UserProfile' %}
{{ object.user.username }}
{% else %}
{% endif %}
{% endwith %}
{% empty %}
{% endfor %}
</div>
I only got the Post result... I'm beginning with Django. Does anyone has an idea to separate search result in order to add different html presentation?

Unable to show Error with self.add_error()

I am having a problem and need your help.
I am making a django website, following information need to be verified. If it pass registration will be success and fail notice will show up. But Mean while there are no problem with success, but Notice won't show up if fail. Here are my form.py, could anyone help me? Much Thanks.
if settings.VERIFY_BY_SFID and all([i in cleaned_data for i in fields]):
number = cleaned_data["mobile_number"]
verified =SignUpFailureAttemptService.verify_mobile_number(number)
if not verified:
self.add_error(None, _("Your application was rejected."))
Template
{% extends 'accounts/login_base.html' %} {% load i18n %} {% load staticfiles %} {% load semanticui %} {% load common_templatetags %} {% block welcome_text %} {% trans 'Create your personal
E-wallet account.' %} {% endblock %} {% block slogan_text %} {% trans 'Buy and sell with protection
Easy and secure to send and receive money' %} {% endblock %} {% block bigger_text %} {% blocktrans %}Already have an account?{% endblocktrans %} {% trans 'Sign in' %} {% endblock %} {% block left_content %}
STEPS:
{% include 'accounts/signup/steps.html' with active_step=step steps=5 %}
{% block form_title %} {{ form_title }} {% endblock %}
{% if messages %} {% for message in messages %}
{{ message }}
{% endfor %} {% endif %}
{% block form_block %}
{% csrf_token %} {{ form.non_field_errors }} {% for hidden_field in form.hidden_fields %} {{ hidden_field.errors }} {{ hidden_field }} {% endfor %} {% for field in form.visible_fields %} {% if field.name == 'date_of_birth' %}
{{ field.label }}*
{% endif %}
{% if field.name == 'tos' %} {% blocktrans %} I agree to {% endblocktrans %} PDS and FSG {{ field }}
{% else %} {{ field }} {% endif %}
{{ field.help_text }}
{{ field.errors }}
{% endfor %}
{% if prev_step %} «   {% trans 'Back' %} {% endif %}
{% if next_step_title %}
{% trans 'NEXT STEP:' %} {{ next_step_title }}
{% endif %}
{% endblock %} {% endblock %} {% block scripts_end %} {% endblock %}

Flask wtforms DecimalField not displaying in HTML

My TextField forms are displaying fine in the html page, but not the DecimalFields. The DecimalFields don't display at all.
My forms.py:
from flask.ext.wtf import Form
from wtforms import TextField, validators, IntegerField, DateField, BooleanField, DecimalField
class EnterPart(Form):
Description = TextField(label='label', description="description", validators=[validators.required()])
VendorCost = DecimalField(label='label',description="cost")
My application.py:
from flask import Flask, render_template, request, redirect, url_for
def index():
form = EnterPart(request.form)
return render_template('index.html', form=form)
My index.html:
{% extends "base.html" %} {% import 'macros.html' as macros %}
{% block content %}
{{ form.hidden_tag() }}
{{ macros.render_field(form.Description, placeholder='Description', type='dbSerial', label_visible=false) }}
{{ macros.render_field(form.VendorCost, placeholder='Category', type='dbSerial', label_visible=false) }}
{% endblock %}
I've been building from the tutorial here:
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
Thanks in advance.
Macros.html didn't have an option for dealing with DecimalField. By adding a rendering option for DecimalField, it now displays correctly.
I changed a block in macros.html from:
<div class="form-group {% if field.errors %}has-error{% endif %} {{ kwargs.pop('class_', '') }}">
{% if (field.type != 'HiddenField' or field.type !='CSRFTokenField') and label_visible %}
<label for="{{ field.id }}" class="control-label">{{ field.label }}</label>
{% endif %}
{% if field.type == 'TextField' %}
{{ field(class_='form-control', **kwargs) }}
{% endif %}
{% if field.errors %}
{% for e in field.errors %}
<p class="help-block">{{ e }}</p>
{% endfor %}
{% endif %}
</div>
to:
<div class="form-group {% if field.errors %}has-error{% endif %} {{ kwargs.pop('class_', '') }}">
{% if (field.type != 'HiddenField' or field.type !='CSRFTokenField') and label_visible %}
<label for="{{ field.id }}" class="control-label">{{ field.label }}</label>
{% endif %}
{% if field.type == 'TextField' %}
{{ field(class_='form-control', **kwargs) }}
{% endif %}
{% if field.type == 'DecimalField' %}
{{ field(class_='form-control', **kwargs) }}
{% endif %}
{% if field.errors %}
{% for e in field.errors %}
<p class="help-block">{{ e }}</p>
{% endfor %}
{% endif %}
</div>

Class view returns object has no attribute 'rindex' error

I'm trying to rewrite my function based view to class based view. It raises this error:
.../test/User1
'UserDetailView' object has no attribute 'rindex'
The problem is probably obvious, I'm new in class based views.
So what to do to be able get any profile using url .../test/username?
My new view:
class UserDetailView(DetailView):
model = User
def get_object(self, queryset=None):
return get_object_or_404(self.model, pk=self.kwargs["pk"])
URLS.PY:
url(r'^test/(?P<username>[a-zA-Z0-9]+)/$', views.UserDetailView(),name="user_detail"),
And template:
{% extends "base.html" %}
{% block content %}
{{ userprofile.as_p }}
{% endblock %}
My old view is this:
def get_user_profile(request, username):
user = User.objects.get(username=username)
jobs = user.jobs.all()
table = MyJobsTable(jobs)
context = {
'my_jobs': table,
"user": user
}
return render(request, 'auth/profiles/my-profile.html', context=context)
And HTML:
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% load render_table from django_tables2 %}
{% block content %}
{% if user.is_authenticated %}
<h3>{% if user.userprofile.is_translator %} Prekladateľský účet: {% else %} Štandardný
účet: {% endif %}{{ user.username }} </h3>
<ul>
<li>Username: {{ user.username }}</li>
<li>First name: {{ user.first_name }}</li>
<li>Last name: {{ user.last_name }}</li>
<li>Email: {{ user.email }}</li>
<li>Telephone: {{ user.userprofile.telephone }}</li>
<li>Languages: {{ user.userprofile.languages.as_p }}</li>
{# TODO: DOPLNIT ATRIBUTY + ked je aj translator#}
</ul>
{% if user.jobs %}
<p>My Jobs</p>
{% render_table my_jobs %}
{% else %}
<p>You have no jobs</p>
{% endif %}
<form class="navbar-form navbar-right" action="/edit-profile" method="get">
<button type="submit" class="btn btn-success">Edit Your Profile</button>
</form>
<form class="navbar-form navbar-right" action="/register-as-translator" method="get">
<button type="submit" class="btn btn-success">Become A Translator</button>
</form>
{% endif %}
{% endblock %}
URLS.PY:
url(r'^profile/(?P<username>[a-zA-Z0-9]+)/$', views.get_user_profile)
The issue is in your urls.py. With a class-based view, you always need to use the as_view classmethod:
url(r'^test/(?P<username>[a-zA-Z0-9]+)/$', views.UserDetailView.as_view(), name="user_detail"),

Listing specific category first in Python

I have a list of categories from DB as following and it works fine + sorted by ID.
{% for category in menu_categories|sort(attribute="id"): %}
<div>
{{ category.name }}
</div>
{% endfor %}
I just need one exception if category='Pizza' exist to list it first.
Unless I misunderstood you, this should do it:
{% for category in menu_categories|sort(attribute="id"): %}
{% if category.name == 'Pizza': %}
<div> {{ category.name }} </div>
{% endif %}
{% endfor %}
{% for category in menu_categories|sort(attribute="id"): %}
{% if category.name != 'Pizza': %}
<div> {{ category.name }} </div>
{% endif %}
{% endfor %}

Categories

Resources