문제

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>

도움이 되었습니까?

해결책 2

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

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

다른 팁

url(r'companies/(?P<comp_id>.+?)/buy(/|)$', views.buy)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top