V
V
Vladisus2015-04-18 14:03:43
Django
Vladisus, 2015-04-18 14:03:43

Direct GET? How to do it?

I have a view that processes a ListView and a form, it uses GET and filters posts based on their tag and displays them on the same page. Here:

class AllView(AjaxListView):

  context_object_name = 'smth'
  template_name = 'blog/all_things.html'
  page_template = 'blog/things.html'

  def get_queryset(self):
      form = TagForm(self.request.GET)
      if form.is_valid():
          self.tag_name = (form.cleaned_data['tag']).lower()
          return self.send_results(self.tag_name)
      else:
          self.tag_name = "All"
          return Fact.objects.all()

  def get_context_data(self, **kwargs):
      context = super(AllView, self).get_context_data(**kwargs)
      if 'TagForm' not in context:
          context['TagForm'] = TagForm()
      context['tag'] = self.tag_name
      return context

  def send_results(self, tag):
      return Fact.objects.filter(tags__slug=tag)

Now, and want to do just the same, but with a simple link. For example, I have and I want to use GET to go to ?tag=food. I did very stupid but it works: url:<a>Food</a>
url(r'^(?P<tag>[\w-]+)$', views.TagView, name='tag'),

view:
def TagView(request, tag):
# change this someday...
  return HttpResponseRedirect(reverse('blog:all') + '?tag={}'.format(tag))

I realize this is stupid, so is there a normal way to do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2015-04-18
@Vladisus

Janga parses the get itself and creates a dictionary in the request

def asdf(request):
    print request.GET

a case of you
#urls
# url(r'^(?P<tag>[\w-]+)$', views.TagView, name='tag'),
url(r'^/$', views.TagView, name='tag'),


# views
     your_get_params_here = self.request.GET

Or you can use the form
<form method=get>
  <input type=hidden name=tag value=Food>
  <input type=submit>

not to write the form by hand, but to apply the mixin, of course
And how to show everything in the template depends on the perversion
Ideally - to do it through the form - you still have a list of parameters somewhere in the model to be stored. And the values ​​of the model can be used as a list of values ​​in the form

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question