How can I handle forward slashes '/' in title when routing in Django templates?












0















My urls.py and template.html look as follows:



urls.py



path('item/<str:title>/', views.ItemDetail.as_view(), name="item_detail'),


template.html



<h3>Title: <a href="{% url 'item_detail' object.title %}">object.title</a></h3> 


Right now, I do not prevent users from creating objects with a forward slash in them. That is probably a separate question



After a user creates an item with a forward slash, let's say 'Do 1/2 the work up front', how can I route to that item using that title? Right now I do not know how to handle the forward slash / and I'm getting the error:




Reverse for 'item_detail' with arguments '('Do 1/2 the work up front',)' not found. 1 pattern(s) tried:
['item/(?P[^/]+)$']




I looked into striptags but that was not what I needed.










share|improve this question























  • Maybe you should use the id of the item instead of it's title.

    – Stephen
    Nov 21 '18 at 20:20











  • That is really helpful, but that is not always practical. Django emphasizes beautiful URLs, so sometimes it is helpful to have text describing the URL, as opposed to numbers and random chars only. Does that make sense?

    – Scott Skiles
    Nov 21 '18 at 20:21











  • Although, your point here is very valid. I don't need this particular form to be "beautiful" or user friendly. That will work to solve this issue in the short term. Thanks.

    – Scott Skiles
    Nov 21 '18 at 20:25






  • 2





    I'm looking at the URL for this post /questions/53419897/how-can-i-handle-forward-slashes-in-title-when-routing-in-django-templates, it seems like they handle that character and probably any other character that will break the URL. I think you'll have to take care of anything that might break the URL. You might want to take a look at this tutorial on slugs. Django has their slugify package as well. It will remove invalid characters from user input - it's called slugify

    – Stephen
    Nov 21 '18 at 20:39











  • Yes, you should link on slugs - store them in a separate field, which is automatically populated from the title field.

    – Daniel Roseman
    Nov 21 '18 at 20:58
















0















My urls.py and template.html look as follows:



urls.py



path('item/<str:title>/', views.ItemDetail.as_view(), name="item_detail'),


template.html



<h3>Title: <a href="{% url 'item_detail' object.title %}">object.title</a></h3> 


Right now, I do not prevent users from creating objects with a forward slash in them. That is probably a separate question



After a user creates an item with a forward slash, let's say 'Do 1/2 the work up front', how can I route to that item using that title? Right now I do not know how to handle the forward slash / and I'm getting the error:




Reverse for 'item_detail' with arguments '('Do 1/2 the work up front',)' not found. 1 pattern(s) tried:
['item/(?P[^/]+)$']




I looked into striptags but that was not what I needed.










share|improve this question























  • Maybe you should use the id of the item instead of it's title.

    – Stephen
    Nov 21 '18 at 20:20











  • That is really helpful, but that is not always practical. Django emphasizes beautiful URLs, so sometimes it is helpful to have text describing the URL, as opposed to numbers and random chars only. Does that make sense?

    – Scott Skiles
    Nov 21 '18 at 20:21











  • Although, your point here is very valid. I don't need this particular form to be "beautiful" or user friendly. That will work to solve this issue in the short term. Thanks.

    – Scott Skiles
    Nov 21 '18 at 20:25






  • 2





    I'm looking at the URL for this post /questions/53419897/how-can-i-handle-forward-slashes-in-title-when-routing-in-django-templates, it seems like they handle that character and probably any other character that will break the URL. I think you'll have to take care of anything that might break the URL. You might want to take a look at this tutorial on slugs. Django has their slugify package as well. It will remove invalid characters from user input - it's called slugify

    – Stephen
    Nov 21 '18 at 20:39











  • Yes, you should link on slugs - store them in a separate field, which is automatically populated from the title field.

    – Daniel Roseman
    Nov 21 '18 at 20:58














0












0








0








My urls.py and template.html look as follows:



urls.py



path('item/<str:title>/', views.ItemDetail.as_view(), name="item_detail'),


template.html



<h3>Title: <a href="{% url 'item_detail' object.title %}">object.title</a></h3> 


Right now, I do not prevent users from creating objects with a forward slash in them. That is probably a separate question



After a user creates an item with a forward slash, let's say 'Do 1/2 the work up front', how can I route to that item using that title? Right now I do not know how to handle the forward slash / and I'm getting the error:




Reverse for 'item_detail' with arguments '('Do 1/2 the work up front',)' not found. 1 pattern(s) tried:
['item/(?P[^/]+)$']




I looked into striptags but that was not what I needed.










share|improve this question














My urls.py and template.html look as follows:



urls.py



path('item/<str:title>/', views.ItemDetail.as_view(), name="item_detail'),


template.html



<h3>Title: <a href="{% url 'item_detail' object.title %}">object.title</a></h3> 


Right now, I do not prevent users from creating objects with a forward slash in them. That is probably a separate question



After a user creates an item with a forward slash, let's say 'Do 1/2 the work up front', how can I route to that item using that title? Right now I do not know how to handle the forward slash / and I'm getting the error:




Reverse for 'item_detail' with arguments '('Do 1/2 the work up front',)' not found. 1 pattern(s) tried:
['item/(?P[^/]+)$']




I looked into striptags but that was not what I needed.







python django






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 20:19









Scott SkilesScott Skiles

7231823




7231823













  • Maybe you should use the id of the item instead of it's title.

    – Stephen
    Nov 21 '18 at 20:20











  • That is really helpful, but that is not always practical. Django emphasizes beautiful URLs, so sometimes it is helpful to have text describing the URL, as opposed to numbers and random chars only. Does that make sense?

    – Scott Skiles
    Nov 21 '18 at 20:21











  • Although, your point here is very valid. I don't need this particular form to be "beautiful" or user friendly. That will work to solve this issue in the short term. Thanks.

    – Scott Skiles
    Nov 21 '18 at 20:25






  • 2





    I'm looking at the URL for this post /questions/53419897/how-can-i-handle-forward-slashes-in-title-when-routing-in-django-templates, it seems like they handle that character and probably any other character that will break the URL. I think you'll have to take care of anything that might break the URL. You might want to take a look at this tutorial on slugs. Django has their slugify package as well. It will remove invalid characters from user input - it's called slugify

    – Stephen
    Nov 21 '18 at 20:39











  • Yes, you should link on slugs - store them in a separate field, which is automatically populated from the title field.

    – Daniel Roseman
    Nov 21 '18 at 20:58



















  • Maybe you should use the id of the item instead of it's title.

    – Stephen
    Nov 21 '18 at 20:20











  • That is really helpful, but that is not always practical. Django emphasizes beautiful URLs, so sometimes it is helpful to have text describing the URL, as opposed to numbers and random chars only. Does that make sense?

    – Scott Skiles
    Nov 21 '18 at 20:21











  • Although, your point here is very valid. I don't need this particular form to be "beautiful" or user friendly. That will work to solve this issue in the short term. Thanks.

    – Scott Skiles
    Nov 21 '18 at 20:25






  • 2





    I'm looking at the URL for this post /questions/53419897/how-can-i-handle-forward-slashes-in-title-when-routing-in-django-templates, it seems like they handle that character and probably any other character that will break the URL. I think you'll have to take care of anything that might break the URL. You might want to take a look at this tutorial on slugs. Django has their slugify package as well. It will remove invalid characters from user input - it's called slugify

    – Stephen
    Nov 21 '18 at 20:39











  • Yes, you should link on slugs - store them in a separate field, which is automatically populated from the title field.

    – Daniel Roseman
    Nov 21 '18 at 20:58

















Maybe you should use the id of the item instead of it's title.

– Stephen
Nov 21 '18 at 20:20





Maybe you should use the id of the item instead of it's title.

– Stephen
Nov 21 '18 at 20:20













That is really helpful, but that is not always practical. Django emphasizes beautiful URLs, so sometimes it is helpful to have text describing the URL, as opposed to numbers and random chars only. Does that make sense?

– Scott Skiles
Nov 21 '18 at 20:21





That is really helpful, but that is not always practical. Django emphasizes beautiful URLs, so sometimes it is helpful to have text describing the URL, as opposed to numbers and random chars only. Does that make sense?

– Scott Skiles
Nov 21 '18 at 20:21













Although, your point here is very valid. I don't need this particular form to be "beautiful" or user friendly. That will work to solve this issue in the short term. Thanks.

– Scott Skiles
Nov 21 '18 at 20:25





Although, your point here is very valid. I don't need this particular form to be "beautiful" or user friendly. That will work to solve this issue in the short term. Thanks.

– Scott Skiles
Nov 21 '18 at 20:25




2




2





I'm looking at the URL for this post /questions/53419897/how-can-i-handle-forward-slashes-in-title-when-routing-in-django-templates, it seems like they handle that character and probably any other character that will break the URL. I think you'll have to take care of anything that might break the URL. You might want to take a look at this tutorial on slugs. Django has their slugify package as well. It will remove invalid characters from user input - it's called slugify

– Stephen
Nov 21 '18 at 20:39





I'm looking at the URL for this post /questions/53419897/how-can-i-handle-forward-slashes-in-title-when-routing-in-django-templates, it seems like they handle that character and probably any other character that will break the URL. I think you'll have to take care of anything that might break the URL. You might want to take a look at this tutorial on slugs. Django has their slugify package as well. It will remove invalid characters from user input - it's called slugify

– Stephen
Nov 21 '18 at 20:39













Yes, you should link on slugs - store them in a separate field, which is automatically populated from the title field.

– Daniel Roseman
Nov 21 '18 at 20:58





Yes, you should link on slugs - store them in a separate field, which is automatically populated from the title field.

– Daniel Roseman
Nov 21 '18 at 20:58












1 Answer
1






active

oldest

votes


















0














In django str accepts everything except / symbol. So according to Django documentation,



path - Matches any non-empty string, including the path separator, '/'. This allows you to match against a complete URL path rather than just a segment of a URL path as with str.



However I recommend you to add a slug field which will convert your url into something like this-is-your-url






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419897%2fhow-can-i-handle-forward-slashes-in-title-when-routing-in-django-templates%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    In django str accepts everything except / symbol. So according to Django documentation,



    path - Matches any non-empty string, including the path separator, '/'. This allows you to match against a complete URL path rather than just a segment of a URL path as with str.



    However I recommend you to add a slug field which will convert your url into something like this-is-your-url






    share|improve this answer




























      0














      In django str accepts everything except / symbol. So according to Django documentation,



      path - Matches any non-empty string, including the path separator, '/'. This allows you to match against a complete URL path rather than just a segment of a URL path as with str.



      However I recommend you to add a slug field which will convert your url into something like this-is-your-url






      share|improve this answer


























        0












        0








        0







        In django str accepts everything except / symbol. So according to Django documentation,



        path - Matches any non-empty string, including the path separator, '/'. This allows you to match against a complete URL path rather than just a segment of a URL path as with str.



        However I recommend you to add a slug field which will convert your url into something like this-is-your-url






        share|improve this answer













        In django str accepts everything except / symbol. So according to Django documentation,



        path - Matches any non-empty string, including the path separator, '/'. This allows you to match against a complete URL path rather than just a segment of a URL path as with str.



        However I recommend you to add a slug field which will convert your url into something like this-is-your-url







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 7:14









        Bidhan MajhiBidhan Majhi

        4991412




        4991412
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419897%2fhow-can-i-handle-forward-slashes-in-title-when-routing-in-django-templates%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            How to fix TextFormField cause rebuild widget in Flutter