Django optional url parameters

I have a Django URL like this: url( r’^project_config/(?P<product>\w+)/(?P<project_id>\w+)/$’, ‘tool.views.ProjectConfig’, name=”project_config” ), views.py: def ProjectConfig(request, product, project_id=None, template_name=”project.html”): … # do stuff The problem is that I want the project_id parameter to be optional. I want /project_config/ and /project_config/12345abdce/ to be equally valid URL patterns, so that if project_id is passed, then I can use … Read more

Class has no objects member

def index(request): latest_question_list = Question.objects.all().order_by(‘-pub_date’)[:5] template = loader.get_template(‘polls/index.html’) context = {‘latest_question_list’:latest_question_list} return HttpResponse(template.render(context, request)) The first line of that function gets an error on Question.objects.all(): E1101: Class ‘Question’ has no ‘objects’ member I’m following the Django documentation tutorial and they have the same code up and running. I have tried calling an instance. Question = … Read more

Why does DEBUG=False setting make my django Static Files Access fail?

Am building an app using Django as my workhorse. All has been well so far – specified db settings, configured static directories, urls, views etc. But trouble started sneaking in the moment I wanted to render my own beautiful and custom 404.html and 500.html pages. I read the docs on custom error handling, and set … Read more

How can I find script’s directory? [duplicate]

This question already has answers here: How do you properly determine the current script directory? (16 answers) Closed 5 years ago. Consider the following Python code: import os print os.getcwd() I use os.getcwd() to get the script file’s directory location. When I run the script from the command line it gives me the correct path … Read more

Django – Rebuild a query string without one of the variables

I have a Django view that processes a GET request. I want to rebuild the query string to include all variables except for one. I was initially using list comprehension: >>> from django.http import QueryDict >>> q = QueryDict(‘a=2&b=4&c=test’) // <— make believe this is request.GET >>> z = QueryDict(”).copy() >>> z.update(dict([x for x in … Read more