Question

I use dynamic URLs in django. It works fine for integer values, and works for strings if the dynamic part is the end if the URL. When there is some other component in the URL after the dynamic variable, say:

url(r'companies/(?P<comp_id>.+)/buy/?$',views.buy)

Now in the views.buy function,

print comp_id 

gives me id1/buy. So, it takes the whole of the remaining URL as the comp_id variable. How do I stop it?

company.html:

<html>
    <head>
            <title>{{ company.name }}</title>
    </head>
    <body>
            Name:{{ company.name }}<br> 
            Worth: {{ company.company_worth }}<br>

            <form action="/companies/{{ company.comp_id }}/buy/" method = "post">
                    {% csrf_token %}
                    {{ form.as_p }}
                    <input type="submit" value="Buy">
            </form>

    </body>

Was it helpful?

Solution 2

If the comp_id value is id1, this would do the trick.

url(r'companies/(?P<pdf>\w+)/buy/?$',views.buy)

OTHER TIPS

url(r'companies/(?P<comp_id>.+?)/buy(/|)$', views.buy)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top