Rookie
Rookie

Reputation: 57

using GET variables with django

I'm just trying to get a minimum working example of GET variable usage in django. Apologies, I'm very very new to django.

At the moment, urls.py looks like:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
     url(r'^test/', views.engage),
]

views.py:

def engage(request):
  return(request.GET)

I'm trying to access an integer variable at domain.com/test/&n=10 but at this stage I keep getting

Exception Type: AttributeError
Exception Value:    
This QueryDict instance is immutable

I've been trying to figure this out for about 2 days now. I've searched pretty widely, but I just don't seem to be getting anywhere. I don't need the answer on a silver platter; links to tutorials etc would be equally appreciated. I feel like there's something pretty core that I'm missing.

Thanks.

Upvotes: 1

Views: 67

Answers (1)

2ps
2ps

Reputation: 15936

If engage is a view, it needs to return a response. Preferably an HTTP response that the user's browser can render.

You can try something like this:

def engage(request):
  n = int(request.GET.get('n', 0))
  return HttpResponse('<html><body>You sent over %s</body></html>' % n)

Also your URL should look like:

domain.com/engage/?n=10

Upvotes: 2

Related Questions