I have a situation (shopping cart checkout sequence) where the workflow used in Django's FormPreview contrib app looks perfect, except I need to have some view logic occur before I call it (I can't call the checkout sequence if the cart is empty, for example). From the docs, it looks like you call FormPreview directly from the urlconf like so:
(r'^post/$', SomeModelFormPreview(SomeModelForm))
...and it calls the overridden done() method for the FormPreview directly (without a view).
Since my urls.py is similar to:
url(r'^checkout/$', 'checkout', {'SSL': settings.ENABLE_SSL }, name = 'checkout'),
and my view is similar to:
def checkout(request):
if cart.empty(request):
cart = urlresolvers.reverse('shopping_cart')
return HttpResponseRedirect(cart)
if request.method == 'POST':
checkoutform = CheckoutFormPreview(CheckoutForm)
This last line is where I'd like to call it, but can't figure out how to wrap it... Suggestions?
It looks like CheckoutFormPreview(CheckoutForm) returns a callable view that you can add to your url config. If you call it in your view, you just need to pass the required request argument. Then return the result.
Putting it together, you have (untested):
if request.method == 'POST':
form_preview_view = CheckoutFormPreview(CheckoutForm)
return form_preview_view(request)
Related
I am trying to call a stored procedure in Django that returns some data based on which batch number you give it. In my database if I write
call InkItUp.InkBatchNumberCallBackk(15137);
I return the data I need. But then I try to use Postman to call the URL I have defined for the view saveInkbatch in my urls.py file It gives me this error:
The view api.views.saveInkbatch didn't return an HttpResponse object. It returned None instead.
Me urls.py look something like this:
path('createsp/', views.saveInkbatch),
and there is the method in the view.py
#csrf_exempt
def saveInkbatch(request):
if request.method == 'POST':
if request.POST.get('batchnumber'):
save=Ink()
save.batchnumber=request.POST.get('batchnumber')
cursor=connection.cursor()
cursor.execute("call InkItUp.InkBatchNumberCallBackk('"+save.batchnumber+"')")
messages.success(request, "The batchnumber "+save.batchnumber+"")
return HttpResponse(request, content_type = 'application/json')
If you know a way to call a stored procedure from MySQl with a class-based view - It would be nice to. I would much rather use class-based views if possibly.
In my project I'm trying to hit a url(which is running in same project) in my view.
so in simplest way I can explain here.
#login_required
def my_api_view(request):
if requests.method == 'GET':
# do stuff
return JsonResponse()
# and its url is `/api/get-info/`
another view which is consuming above api
#login_required
def show_info(request):
url = get_base_url + /api/get-info/ # http://localhost:8000/api/get-info/
r = requests.get(url)
return HttpResponse(r.json())
Now I have to use same session (login required) so when I hit the url using requests it complains that user is not loggedin which is obviously correct.
How can I do this elegantly and efficienty. session use of logged in user. (I dont want to call the view as a function, I want to hit the api-url end point and consume.
PS: we have something similar in django Test self.client.get(...)
Just call that view function and pass the request object as parameter to it.
#login_required
def show_info(request):
r = my_api_view(request)
return HttpResponse(r.json())
Or a better option would be to simply separate the logic into a separate function, as mentioned by #koniiiik in the comments.
EDIT: Or if you really want to hit the URL endpoint, you can simply pass on the cookie values to the request you make.
#login_required
def show_info(request):
url = get_base_url + "/api/get-info/" # http://localhost:8000/api/get-info/
r = requests.get(url, cookies=request.COOKIES)
return HttpResponse(r.json())
I am trying to use Django's pagination for class based views, as described in the docs.
In my urls.py, I have:
url(r'^clues/page(?P<page>[0-9]+)/$', views.ClueIndexView.as_view())
The docs tell me I should be able to access this with an url like:
/clues/?page=3
But that always fails with a 404.
Instead, /clues/page3/ works....but that isn't what I want...I want to use ?page=3.
What am I doing wrong?
EDIT:
I'm handling it with a class-based view, like so:
class ClueIndexView(ListView):
context_object_name = 'clue_list'
template_name = 'clue_list.html'
queryset = Clue.objects.all()
paginate_by = 10
You should do something like this:
url(r'^clues/$')
def clues(request):
if request.method == 'GET':
page = request.GET.get('page')
...
all GET info passed after '?' like your page '?page=n' stored in request.GET dictionary
My url was bad. I found the docs to be a bit confusing. My url needs to be just
url(r'^clues/$', views.ClueIndexView.as_view()
Works now.
I want to call a function from another function in django. The function looks like this
def main_view(request):
if request.method == 'GET':
I dont want to have the url to main_view open so that user can access this url directly, instead they should face a login page(just a lot of checkboxes that the four right ones should be choosen) and after that submit, they should come to the template that the main_view function is rendering.
But, how do I actually call function internal that need a request GET input?
Is there a way to do a GET request when calling the function?
//Mikael
You can just pass the request from your other view into that one, like this:
def my_public_view(request):
if user_passed_checkbox_test():
return main_view(request)
Code
#register.inclusion_tag('template.html', takes_context=True)
def include_client_side_bar(context):
return {
'STATIC_URL': settings.STATIC_URL,
}
My code is will be something like this, I want to access request.user object inside this function, but I can't get it.
In the debugger, I can see these variables.
As far as I can recall, I have made this successfully in django 1.3, did I miss something?
Make sure you've included the request context processor in your TEMPLATE_CONTEXT_PROCESSORS setting, and that your view is rendered with a RequestContext.
try with request = context.get('request', None) if request key doesn't exists assign None value.
http://docs.python.org/library/stdtypes.html#dict.get
Update:
Also you can pass user to inclusion_tag, with something like this
# In your template_tag
#register.inclusion_tag('template.html', takes_context=True)
def include_client_side_bar(context, user):
if user:
pass # do something
return {
'STATIC_URL': settings.STATIC_URL,
}
# in your template
{% include_client_side_bar user=request.user %}