django pass parameters from form to view - python

I have a form that accepts a single date, or a date range. The date is then passed to a subsequent view that queries the DB and produces a result. I've managed to pass the single date via the URL
but I have yet to find a means to pass the date range from the form to the subsequent view where the query is run.
( Note: I understand that the indentation of the form presented below is off. For some this is my first post and I'm not yet wise to all the subtleties of stack overflow)
class Date_rangeFRM(forms.Form):
Single_date = forms.DateField(required=False)
Date_start = forms.DateField(required=False)
Date_end = forms.DateField(required=False)
def date_range(request):
context = RequestContext(request)
if request.method == 'POST':
form = Date_rangeFRM(request.POST)
print "valid", form.is_valid()
print "form", str(form)
if form.is_valid():
#print "form", str(form)
Single_date = form.cleaned_data['Single_date']
Date_start = form.cleaned_data['Date_start']
Date_end = form.cleaned_data['Date_end']
if Single_date != "":
#print "redirecting to Dav View:", '/counter/%02d-%02d-%02d/Day_view/' %(Single_date.year,Single_date.month,Single_date.day)
return HttpResponseRedirect('/counter/%02d-%02d-%02d/Day_view/' %(Single_date.year,Single_date.month,Single_date.day))
else:
#****passing Magic ********
return HttpResponseRedirect('/counter/History_View/')
else:
print form.errors
else:
form = Date_rangeFRM()
return render_to_response('counter/historyFRM.html', {'form':form}, context)
def History_View(request):
context = RequestContext(request)
#****** Receiving magic********
date_start = form.cleaned_data['date_start']
date_end = form.cleaned_data['date_end']
context['meal_list'] = meal_list
context['comp_list'] = comp_list
context['day_list'] = day_list
dt_range = []
day_list = RecordedDays.objects.filter(profile= request.user.userprofile,
Date__gte = date_start, Date__lte = date_end )
for day in day_list:
dt_range.append(day.Date)
meal_list = MealType.objects.filter(
newDay__profile = request.user.userprofile,
newDay__Date__gte = date_start, newDay__Date__lte = date_end )
#newDay__Date= date( int(year), int(month), int(day) ) )
for meal in meal_list:
dt_range.append([ meal.get_mealNum_display(), meal.Mealtotal() ])
comp_list =Ingredient.objects.filter(numMeal = meal)
for comp in comp_list:
dt_range[-1].append([comp.component, comp.comp_total()] )
return render_to_response('counter/history_view.html', context
Also I'm not entirely sure I'm using the __lte, __gte feature correctly. If you could please inspect it. Much appreciated.
Thanks One and all for your Help.

Related

How to include the start date and end date while filtering in django

In views.py:
if request.method == "POST":
from_date = request.POST.get("from_date")
f_date = datetime.datetime.strptime(from_date,'%Y-%m-%d')
print(f_date)
to_date = request.POST.get("to_date")
t_date = datetime.datetime.strptime(to_date, '%Y-%m-%d')
print(t_date)
check_box_status = request.POST.get("new_records", None)
print(check_box_status)
drop_down_status = request.POST.get("field")
print(drop_down_status)
if check_box_status is None:
get_records_by_date = Scrapper.objects.filter(start_time__range=(f_date, t_date))
The following code get_records_by_date = Scrapper.objects.filter(start_time__range=(f_date, t_date)) is unable to include the t_date. Is there any solution to include the t_date?
Breaks.objects.filter(date__range=["2011-01-01", "2011-01-31"])
Or if you are just trying to filter month wise:
Breaks.objects.filter(date__year='2011',
date__month='01')
Please reply to this message ,If it doesn't work.

How Can I sum the Result of a Django Date Range Result in Views

I want to add all the amount field during a date range query search. I have an Income Model with date and amount fields among others. And any time a user select between two dates, I want the amount fields of the query results added as total.
Here is what I have tried:
def SearchIncomeRange(request):
listIncome = Income.objects.all()
searchForm = IncomeSearchForm(request.POST or None)
if request.method == 'POST':
listIncome = Income.objects.filter(
description__icontains=searchForm['description'].value(),
date__range=[
searchForm['start_date'].value(),
searchForm['end_date'].value()
]
)
else:
searchForm = IncomeSearchForm()
paginator = Paginator(listIncome, 5)
page = request.GET.get('page')
paged_income = paginator.get_page(page)
context = {
'searchForm':searchForm,
}
return render(request, 'cashier/search_income_range.html', context)
I am able to get the correct search result but getting the total I don't know how to use the SUM in the above query and pass the total in pagination. So Someone should please help me out. Thanks
from django.db.models import Sum
total_amount = listIncome.aggregate(total=Sum('amount'))
where listIncome is your queryset
Edit:
You should pass queryset in pagination with filtered queryset if any filter you apply.
I changed your written code but you can write this code in a good way.
def SearchIncomeRange(request):
listIncome = Income.objects.all()
searchForm = IncomeSearchForm(request.POST or None)
if request.method == 'POST':
# you can get filter value by your form data
post_data = request.POST
description = post_data['description']
start_date = post_data['start_date']
end_date = post_data['end_date']
else:
# you can get filter value by your query params
query_params = request.GET
description = query_params.get('description')
start_date = query_params.get('start_date')
end_date = query_params.get('end_date')
# Apply filter before pagination
listIncome = listIncome.filter(
description__icontains=description,
date__range=[start_date, end_date]
)
# calculate total_amount
total_amount = listIncome.aggregate(total=Sum('amount'))
paginator = Paginator(listIncome, 5)
page = request.GET.get('page')
paged_income = paginator.get_page(page)
# you can access total_amount in template by passing in context data
context = {
'searchForm':searchForm,
'total_amount': total_amount
}
return render(request, 'cashier/search_income_range.html', context)

Nesting If statements to detect request value in Django views

I am trying to get third button to work on a simple app I am making. I set up if statements to detect the value of the request for two buttons, but I am getting stuck on how to configure the logic statement to get a third parameter through.
My issue is that I need to have the form filled with any data before the subscriptions button will work properly. When the form is blank the app will indicate the form needs to be filled out. I am not familiar with javascript to have the buttons run from that. My hope is that I can learn how to configure the if statement to have the subscriptions call work without the form being filled out.
Below is the UI as and the views.py def. The Amount and Delete functions work fine. It is just the subscription button that won't work until I put literally any data into the two text fields fields.
def usage(request):
if request.method == 'POST':
form = UsageForm(request.POST)
if form.is_valid():
if request.POST.get('subscription') == 'Subscription':
dt = datetime.today()
month = dt.month
year = dt.year
day = calendar.monthrange(year, month)[1]
eom = str(month) + "/" + str(day) + "/" + str(year)
anchor = int(time.mktime(datetime.strptime(eom, "%m/%d/%Y").timetuple()) + 68400)
cust_list = json.loads(str(stripe.Customer.list(limit=100)))
for item in cust_list['data']:
try:
print(item['subscriptions']['data'][0]['id'], item['email'], item['id'])
except:
stripe.Subscription.create(
customer=item['id'],
items=[{'plan': 'plan_DFnNVzXKJ2w0Ad'}],
billing_cycle_anchor=anchor,
)
if request.POST.get('delete') == 'Delete':
cust_info = custinfo.objects.all()
cust_info.delete()
all_custinfo_items = custinfo.objects.all()
else:
Sub_ID = form.cleaned_data['Sub_ID']
amount = form.cleaned_data['amount']
stripe_data = stripe.SubscriptionItem.list(subscription=Sub_ID)
sub_item = stripe_data["data"][0]["id"]
stripe.UsageRecord.create(
quantity=amount,
timestamp=int(time.time()),
subscription_item=sub_item,
action='increment')
form.save()
print("Last Ran: ", Sub_ID, amount)
all_custinfo_items = custinfo.objects.all()
return render(request, 'form.html', {'form': form, 'custinfo_items': all_custinfo_items})
else:
form = UsageForm()
# all_subid_items = custinfo.objects.filter(Sub_ID__startswith='sub')
return render(request, 'form.html', {'form': form})

request from form to raw database on django

I new with python and django, currently I little a bit confused about how to do my project.
Scenario is simple:
User by using form on fronted is requested weekly statistic of customers and should get list of statistic per customer on selected period (year, week)
So far I able to query data from raw database, function for count statistic start_date and end_date of selected week working fine.
in my models.py
def WeekRange(year, week):
d = date(year, 1, 1)
delta_days = d.isoweekday() - 1
delta_weeks = week
if year == d.isocalendar()[0]:
delta_weeks -= 1
delta = timedelta(days=-delta_days, weeks=delta_weeks)
weekbeg = datetime.datetime.combine(d, datetime.time(00, 00, 01)) + delta
delta2 = timedelta(days=6-delta_days, weeks=delta_weeks)
weekend = datetime.datetime.combine(d, datetime.time(23, 59, 59)) + delta2
cursor = connections['nocdb'].cursor()
cursor.execute("SELECT DISTINCT (p.name) AS platform, count(e.id ) AS count FROM event e, lu_platform p WHERE e.platform_id = p.id AND e.sourcetype_id = 1 AND e.event_datetime BETWEEN %s AND %s AND e.sender_id NOT IN ( 759, 73 ) GROUP BY p.name ORDER BY p.name", [weekbeg, weekend] )
results = cursor.fetchall()
return results
view.py
def WeekRequestForm(request):
form = WeekSelection()
year = request.POST.get("q_year", "")
week = request.POST.get("q_week", "")
currstat_b = [year, week]
weekstat = WeekRange(*currstat_b)
return render_to_response('form.html', {'weekstat': weekstat, 'form': form}, context_instance=RequestContext(request))
forms.py
YEARCHOICE = tuple((str(n), str(n)) for n in range(2011, datetime.now().year + 1))
WEEKCHOICE = range(1, 53)
class WeekSelection(forms.Form):
q_year = forms.ChoiceField(label='Year', choices=YEARCHOICE, initial=(datetime.today().isocalendar()[0]))
q_week = forms.ChoiceField(label='Week', choices=zip(WEEKCHOICE, WEEKCHOICE), initial=(datetime.today().isocalendar()[1]))
With followings I get "an integer is required"
after replace currstat_b = [2013, 48] everything working fine and show me statistick of week 48.
Problem is how to get selected numbers from form to argument in weekstat = WeekRange(*currstat_b)
Also I not sure if is better put raw data function in views or in models as is now.
I will be appreciated for any tip
Maybe passing data through form could help, try this:
def WeekRequestForm(request):
form = WeekSelection()
weekstat = None
if request.method == "POST":
form = WeekSelection(request.POST)
if form.is_valid():
year = int(form.cleaned_data["q_year"])
week = int(form.cleaned_data["q_week"])
currstat_b = [year, week]
weekstat = WeekRange(*currstat_b)
return render_to_response('form.html', {'weekstat': weekstat, 'form': form},
context_instance=RequestContext(request))

Django form search

I am making a search form in django, and I am struggling with processing the form in my view.
My code:
class SearchForm(forms.Form):
name = forms.CharField(label="Name", max_length=64, required=False)
...
<a few other fields>
def search(request):
if request.method == 'POST':
form = SearchForm(request.POST)
if form.is_valid():
name = form.cleaned_data['name']
qdict = { 'name': name}
q_objs = [Q(**{qdict[k]: form.cleaned_data[k]}) for k in qdict.keys() if form.cleaned_data.get(k, None)]
search_results = Group.objects.select_related().filter(*q_objs)
response = {'success' : search_results}
return HttpResponse(simplejson.dumps(response, ensure_ascii=False), mimetype='application/javascript')
else:
form = SearchForm()
return render_to_response("main/search.html", {'form': form},
context_instance=RequestContext(request))
I get this error:
Cannot resolve keyword u'NAME' into field. Choices are: date_submitted, id, name, parameters.
I realized that this field is in unicode and tried converting it with str(...) or with encode('ascii',...), but it still gives me the same error. I am new to django, so any help would be appreciated.
Thanks
To find your error code replace this code:
name = form.cleaned_data['name']
qdict = { 'name': name}
q_objs = [Q(**{qdict[k]: form.cleaned_data[k]}) for k in qdict.keys() if form.cleaned_data.get(k, None)]
search_results = Group.objects.select_related().filter(*q_objs)
By this one:
q=None
for k,v in form.cleaned_data.items():
if q:
q &= Q( k = v )
else:
q = Q( k = v )
search_results = Group.objects.select_related().filter( q )
But, to have really control over your query, you need to write condition by condition:
qs = []
name = form.cleaned_data['name']
if name:
q_name = Q( name__contains = name )
qs.append(q_name)
fromDate = form.cleaned_data['fromDate']
if fromDate:
q_from = Q( date__gte = fromDate )
qs.append(q_from)
toDate = form.cleaned_data['toDate']
if toDate:
q_toDate = Q( date__gte = toDate )
qs.append(q_toDate)
q=None
for x in qs:
if q:
q &= x
else:
q = x
search_results = Group.objects.select_related().filter(q)
It complains about the (uppercase) NAME field, and judging by the error message's format, it is the query that triggers it. I can't really tell from your code, but at some point I think you execute the equivalent of the following:
Group.objects.filter(NAME='some_value')
If the Group model (which you didn't post, so this is an educated guess) contains a lowercase name field, the above query will generate the error you posted as it tries to access the uppercase NAME field.
So I guess it boils down to: what does the final query look like? For getting information on that, danihp's comment already provided a good breakdown for how to determine this.

Categories

Resources