1

I have a little problem with my django website's urls.

In fact I have:

urlpatterns = i18n_patterns('',
  url(r'^test/(?P<variable_name>\w+)/$', views.testUrl),

I have a dynamic page created with the name "test". redirected from the view after some treatment :

def testUrl(request, variable_name):
  current_site = Site.objects.get_current()
  urlTest = HttpRequest.get_full_path(request)
  parseIt = variable_name.split("/")
  ...
  for x in parseIt:
    if x == 'm':
        if ToFind[:1] == 'm':
            ID = ToFind[1:]
        else:
            ID = ToFind
        try:
            context_ret = GetId(x, api_url, ID, api_key, context_ret)
        except Exception as err:
            context_ret['message'] = err
            return render(request, 'base_templates/test.html', context_ret)
    elif x == 'z':
        try:
            context_ret = GetId(x, api_url, ToFind, api_key, context_ret)
        except Exception as err:
            context_ret['message'] = err
            return render(request, 'base_templates/test.html', context_ret)

  return render(request, 'base_templates/test.html', context_ret)

So if I type mydomain.org/test/ I have my dynamic page showing. Perfect.

But if I do mydomain.org/test/{whatever} I have the test.html template rendered but not the dynamic page ! Thus, the problem is that I have dynamic plugins within this dynamic page, I need to - whatever is behind test/ - use the same dynamic page. And not juste the template.

Without changing the url..

Is there a way of doing it ?

Edit:

here is an example of a call: domain.org/test/1923/

Community
  • 1
  • 1
Jay Cee
  • 1,855
  • 5
  • 28
  • 48

1 Answers1

2

Since you want to wirk with {whatever} comming from /test/{whatever}, you need to declare a variable in your url or pass {whatever} via GET variable.

Declaring a variable in url:

Change your url definition like this

...
url(r'^test/(?P<variable_name>\w+)/$', views.testUrl),
...

And catch it in the view:

def testUrl(request, variable_name):
    # Now if you call '/test/hithere/'
    # variable_name will take the value 'hithere'
    # you could do -> parseIt = variable_name
    ...

Passing {whatever} via GET variable

You can always call you url like this:

mydomain.org/test/?var=hithere

And the in your view:

def testUrl(request):
    parsetIt = request.GET.get('var')
    ...
Community
  • 1
  • 1
Gocht
  • 9,924
  • 3
  • 42
  • 81
  • just to be sure, variable_name is the default thing or should I write something as "(a-z)(A-Z)(0-9)" ? – Jay Cee Dec 17 '15 at 16:53
  • @JayCee You should replace variable_name with a verbose name for your variable. – Gocht Dec 17 '15 at 16:55
  • Ok because this variable is independent of me. I mean it can be /test/f/numbers or /test/k/m98324 etc – Jay Cee Dec 17 '15 at 16:56
  • @JayCee you should use the right regex depending on what value do you expect to get in variable_name, check the first link I provided in my answer – Gocht Dec 17 '15 at 16:58
  • So I tried it, but I am back to my original problem haha. It's weird, because the template call is the good one, `base_templates/test.html` but plugins are not showing. As if it was a different page or just the template requested, ignoring the dynamic page created. – Jay Cee Dec 17 '15 at 17:11
  • @JayCee Add your changes to your post – Gocht Dec 17 '15 at 17:11
  • @JayCee Please add an example, how are you calling your url, what value are you passing. – Gocht Dec 17 '15 at 17:17