Django, url does not match - python

This may be a duplicate question but I can't find any answer here on SO.
I'm trying to write a method able to take two different models. I have a Post model and a Comment model, and I want the vote_up method to handle voting for both of these.
views.py
def vote_up(request, obj): #portotype not working atm...
if isinstance(obj, Post):
o = Post.objects.get(id=obj.id)
elif isinstance(obj, Comment):
o = Comment.objects.get(id=obj.id)
else:
return HttpResponseRedirect(request.META.get('HTTP_REFERER')) #add 'something went wrong' message
o.votes += 1
o.save()
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
urls.py
urlpatterns = patterns('',
url(r'^vote_up/(?P<obj>\d+)/$', 'post.views.vote_up'),
url(r'^post_vote_down/(?P<post_id>\d+)/$', 'post.views.post_vote_down'), # works fine no instance check here, using separate methods for Post/Comment
url(r'^comment_vote_down/(?P<comment_id>\d+)/$', 'post.views.comment_vote_down'),
)
The errors i get is listing my existing urls and:
The current URL, post/vote_up/Post object, didn't match any of these.
or
The current URL, post/vote_up/Comment object, didn't match any of these.
I'm guessing \d+ is the villain but can't seem to find the right syntax.

As Burhan says, you can't send an object in a URL, only a key. But another approach would be to include the model in the URLconf itself: you can use a single pattern, but capture the model name there as well.
url(r'^(?P<model>post|comment)_vote_down/(?P<pk>\d+)/$', 'post.views.post_vote_down'),
)
then in the view:
def vote_up(request, model, pk):
model_dict = {'post': Post, 'comment': Comment}
model_class = model_dict[model]
o = model_class.objects.get(pk=pk)

Change this:
url(r'^vote_up/(?P<obj>\d+)/$', 'post.views.vote_up'),
To
url(r'^vote_up/(?P<obj>[-\w]+)/$', 'post.views.vote_up'),
\d+ means only integer.

You cannot send an object in the URL, you need to send the primary key and then retrieve the corresponding object from the database.
def vote_up(request, obj): #portotype not working atm...
try:
o = Post.objects.get(pk=obj)
except Post.DoesNotExist:
o = Comment.objects.get(pk=obj)
except Comment.DoesNotExist:
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
o.votes += 1
o.save()
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

You need to differentiate between comment and post in your url:
urls.py
url(r'^vote_up/(?P<object_name>\w+)/(?P<id>\d+)/$', 'post.views.vote_up'),
views.py
def vote_up(request, object_name, id):
if object_name == 'comment':
# get id for comment
if object_name == 'post':
# get id for post
else:
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

Related

How to export django admin changelist as csv

I want to export my changelist (fields in list_display) as csv. I used the code from https://books.agiliq.com/projects/django-admin-cookbook/en/latest/export.html But it creates csv for the model fields. But in my case, I want to export changelist as csv, not the model fields.
Also, note that most of the fields in changelist(list_display) are calculated fields in Admin.
This is my code
class ExportCsvMixin:
def export_as_csv(self, request, queryset):
meta = queryset.model._meta
field_names = [field.name for field in meta.fields]
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename={}.csv'.format(meta)
writer = csv.writer(response)
writer.writerow(field_names)
for obj in queryset:
row = writer.writerow([getattr(obj, field) for field in field_names])
return response
export_as_csv.short_description = "Export Selected"
class MyAdmin(admin.ModelAdmin, ExportCsvMixin):
list_display = ('field1',
'field2'
)
list_filter = ('field2')
actions = ["export_as_csv"]
def field1(self, obj):
<return logic here>
def field2(self, obj):
<return logic here>
NOTE:
field1 and field2 are calculated fields, and not model fields.
My model is a proxy model. But I don't think it would any difference in this case.
I want the csv to contain data for field1 and field2only, as they are in my changelist.
May be the trick would be to somehow point the queryset to that of changelist. but how do that ? Or if someone can suggest some other solution or even api to achieve this goal?
I had the same issue and I managed to hack it this way.
I looked into the ModelAdmin base class and I found the function responsible for handling actions methods, it's called response_action, I looked into it and changed the queryset it return what I need.
Let say you have a query set that return 'field1' and 'field2'.
Here is how I edited the function to return a custom queryset:
def response_action(self, request, queryset):
"""
Handle an admin action. This is called if a request is POSTed to the
changelist; it returns an HttpResponse if the action was handled, and
None otherwise.
"""
# There can be multiple action forms on the page (at the top
# and bottom of the change list, for example). Get the action
# whose button was pushed.
try:
action_index = int(request.POST.get('index', 0))
except ValueError:
action_index = 0
# Construct the action form.
data = request.POST.copy()
data.pop(admin.helpers.ACTION_CHECKBOX_NAME, None)
data.pop("index", None)
# Use the action whose button was pushed
try:
data.update({'action': data.getlist('action')[action_index]})
except IndexError:
# If we didn't get an action from the chosen form that's invalid
# POST data, so by deleting action it'll fail the validation check
# below. So no need to do anything here
pass
action_form = self.action_form(data, auto_id=None)
action_form.fields['action'].choices = self.get_action_choices(request)
# If the form's valid we can handle the action.
if action_form.is_valid():
action = action_form.cleaned_data['action']
select_across = action_form.cleaned_data['select_across']
func = self.get_actions(request)[action][0]
# Get the list of selected PKs. If nothing's selected, we can't
# perform an action on it, so bail. Except we want to perform
# the action explicitly on all objects.
selected = request.POST.getlist(admin.helpers.ACTION_CHECKBOX_NAME)
if not selected and not select_across:
# Reminder that something needs to be selected or nothing will
# happen
msg = _("Items must be selected in order to perform "
"actions on them. No items have been changed.")
self.message_user(request, msg, messages.WARNING)
return None
if not select_across:
##### change this line with your queryset that return field one and two
queryset = 'your_queryset_with_field'
response = func(self, request, queryset)
# Actions may return an HttpResponse-like object, which will be
# used as the response from the POST. If not, we'll be a good
# little HTTP citizen and redirect back to the changelist page.
if isinstance(response, HttpResponseBase):
return response
else:
return HttpResponseRedirect(request.get_full_path())
else:
msg = _("No action selected.")
self.message_user(request, msg, messages.WARNING)
return None
Check in this function the part I command with #####
redefine that function in the class that inherits from the model admin class
The function returns a custom queryset , now you can edit your export_as_csv_function according to that.
def export_as_csv(self, request, queryset):
field_names = ["field_one", "field_two"]
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename={}.csv'.format(
'working_hours')
writer = csv.writer(response)
writer.writerow(field_names)
for obj in queryset:
writer.writerow([obj.get(field) for field in field_names])
return response
That it and you can go and download your CSV with the customs field.
I hope it's not too late and this helps other folks with the same issue.

Why the url works with parameter pk and not pk2?

When I try to send pk2 or any other argument, it raises an AssertionError.
What I mean is this that the url
path('grade/<str:pk>/', IndividualGrade.as_view(), name="get-grade")
doesn't throw an error while the one below causes an error:
path('grade/<str:pk2>/', IndividualGrade.as_view(), name="get-grade")
My view is fairly simple as below:
class IndividualGrade(generics.RetrieveUpdateDestroyAPIView):
''' PUT/GET/DELETE grade/{grade:pk}/ '''
queryset = Grade.objects.all()
serializer_class = GradeSerializer
def put(self, request, *args, **kwargs):
try:
g1 = Grade.objects.get(grade=kwargs["pk"])
serializer = GradeSerializer(g1, data=request.data)
flag = 0
except Grade.DoesNotExist: # Create a new grade if a grade doesn't exist
g1 = Grade.objects.get(grade=kwargs["pk"])
serializer = GradeSerializer(g1, data=request.data)
flag = 1
if serializer.is_valid():
# call update/create here
else:
return Response(serializer.errors)
return Response(serializer.data, status=status.HTTP_200_OK)
I realized pk2 in the url works if I write my own get function (tried in another view), but I don't know how to fix this without writing my own get. While this have been discussed here. But I am still not sure how to fix it without writing my own get.
you need to add
lookup_field = 'pk2'
when you are using something else than pk, which is inbuilt for lookup . when you want something else in the url you need to mention that.

pass value from one class/function to another class/function

I have written two class one for posting datas for payment and other one to show payment-successful message with order_id. I am sending order id from first function and i want to catch this id to show in my payment-successful template.
class ApiVIew(TemplateView):
template_name = 'payment.html'
def post(self,request):
r = requests.post(url='www.randomsite.com',params = {'authToken':'12345','card_no':'1234','card_cvv':'****'})
return HttpResponse(json.dumps({'response':r.json(),'status':'ok'}))
I call this class is ajax and parse there,so if r gives no error then i redirect(window.location=localhost:8000/success) to the success-payment.html page. so response gives me a json data:
{'isSuccess': 1, 'order_id': 1cq2,}
so i want to get this order_id and pass it to another function/class written below.
def payment_successfullView(request):
return render(request,'payment-successfull.html')
How can i achieve so? Thanks in advance.
1. Most simple way
urls.py:
...
path('<str:order_id>/success/', views.payment_successfullView, name='success'),
...
Views:
from django.shortcuts import redirect, reverse
class ApiVIew(TemplateView):
template_name = 'payment.html'
def post(self, request):
r = requests.post(url='www.randomsite.com',params = {'authToken':'12345','card_no':'1234','card_cvv':'****'})
if r.isSuccess:
return redirect(reverse('success', args=(r.order_id, )))
# do your stuff in case of failure here
def payment_successfullView(request, order_id):
return render(request,'payment-successfull.html', {
'order_id': order_id,
})
2. Another method using sessions:
urls.py:
...
path('success/', views.payment_successfullView, name='success'),
...
Views:
from django.shortcuts import redirect, reverse
from django.http import HttpResponseForbidden
class ApiVIew(TemplateView):
template_name = 'payment.html'
def post(self, request):
r = requests.post(url='www.randomsite.com',params = {'authToken':'12345','card_no':'1234','card_cvv':'****'})
if r.isSuccess:
request.session['order_id'] = r.order_id # Put order id in session
return redirect(reverse('success', args=(r.order_id, )))
# do your stuff in case of failure here
def payment_successfullView(request):
if 'order_id' in request.session:
order_id = request.session['order_id'] # Get order_id from session
del request.session['order_id'] # Delete order_id from session if you no longer need it
return render(request,'payment-successfull.html', {
'order_id': order_id,
})
# order_id doesn't exists in session for some reason, eg. someone tried to open this link directly, handle that here.
return HttpResponseForbidden()
Ok, I think the best answer points you in the right direction and let you figure out the fun part.
Tips:
Your APIView has to redirect to payment_successfullView
You have the order_id so you could use a DetailView
If you want to display a list of orders (order_id's) use ListView
I think using those tips, you'll be fine. Happy coding.
Note
You might want to read about Form views also, such view has an attribute called success_url. Ring a bell?

Django multiple forms on one view

I have a Django template that has data from a few different model types combining to make it. A dashboard if you will. And each of those has an edit form.
Is it best to process all those forms in one view as they are posted back to the same place and differentiating between them by a unique field like below?
Or if having lots of different dedicated avenues is the way forward? Thanks for any guidance
class ProjectDetail(DetailView):
template_name = 'project/view.html'
def get_object(self):
try:
return Project.objects.filter(brief__slug=self.kwargs['slug']).filter(team=get_user_team(self.request)).first()
# add loop to allow multiple teams to work on the same brief (project)
except Exception as e:
project_error = '%s (%s)' % (e.message, type(e))
messages.error(self.request, 'OH NO! %s' % project_error)
return redirect('home')
def get_context_data(self, **kwargs):
project = self.get_object()
context = dict()
context['project'] = project
context['milestone_form'] = MilestoneForm(initial={'project': project})
context['view'] = self
return context
def post(self, request, *args, **kwargs):
# get the context for the page
context = self.get_context_data()
try:
# switch for each of the form types on the team profile page (shown if member)
if 'milestone_form_submit' in request.POST:
project=self.get_object()
# set date arbitrarily to half way to brief deadline
brief = Brief.objects.get(project=project)
last_milestone = self.milestones().last()
milestone_del_date = last_milestone.del_date + timedelta(days=7)
new_milestone = Milestone(
project=project,
title_text=request.POST.get('title_text'),
del_date=milestone_del_date,
)
try:
new_milestone.save()
messages.success(self.request, "Excellent! New delivery popped on the bottom of the list")
except Exception as e:
# pass the erroring form back in the context if not
form = MilestoneForm(request.POST)
context['milestone_form'] = form
messages.error(self.request, "OH NO! Deadline didn't save. Be a sport and check what you entered")
elif 'milestone-edit-date-form-submit' in request.POST:
# get object from db
milestone = Milestone.objects.get(pk=request.POST['id'])
# update del_date field sent
milestone.del_date = request.POST['del_date']
# save back to db
milestone.save()
messages.success(self.request, "Updated that delivery right there!")
elif ...
except Exception as e:
messages.error(self.request, "OH NO! Deadline didn't save. Be a sport and check what you entered")
return render(request, self.template_name, context)
You can use mixins in order to solve your problem.
Example from the gist https://gist.github.com/michelts/1029336
class MultipleFormsMixin(FormMixin):
"""
A mixin that provides a way to show and handle several forms in a
request.
"""
form_classes = {} # set the form classes as a mapping
def get_form_classes(self):
return self.form_classes
def get_forms(self, form_classes):
return dict([(key, klass(**self.get_form_kwargs())) \
for key, klass in form_classes.items()])
def forms_valid(self, forms):
return super(MultipleFormsMixin, self).form_valid(forms)
def forms_invalid(self, forms):
return self.render_to_response(self.get_context_data(forms=forms))
As you can see, when you inherit from this class, you can handle multiple forms simultaneously. Look at the gist's code and adapt it to your problem.
Look at this answer

MultiValueDictKeyError at /addbook/

def addbook(request):
if request.method == 'POST':
book_name =request.POST['book_name']
Book = Book.objects.get()
Book.save()
return render_to_response('book_detail.html', {'books': books},context_instance=RequestContext(request))
else:
return render_to_response('addbook.html',context_instance=RequestContext(request))
def book_detail(request):
return render(request, 'book_detail.html')
the above is my view.py i am getting this error"MultiValueDictKeyError at /addbook/"
please help me
That error means that 'book_name' isn't in your POST data.
If you want to handle that case, you can use book_name = request.POST.get('book_name'), which will default book_name to None if it isn't in the POST data.
If not, you need to make sure the form has an input called 'book_name'.

Categories

Resources