ShellRox ShellRox - 1 year ago 83
Python Question

Django: Make query using url ( Assigning variable using url )

I am trying to make a query system for my website, i think the best way and the most compact would be to assign search variable using url pattern.

So for example, i want to search objects of model


User sends HttpRequest to following url:"admin"

Now HttpRequest is also sent to
view, we somehow get
variable data.

def search(request):
for query in User.objects.all():
if q in query: # < We somehow need to get data of 'q'.
return HttpResponse(q)

Since i have admin in
, this should return HttpResponse of 'admin'.

How can this url pattern be made? So i can assign
variable from the url and then send it to system to find it?

Answer Source

You can capture named strings from URLs like this:

urlpatterns = [

def page(request, num="1"):
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download