2

say i have a variable :

a=['https://www.google.com','https://www.go.com','https://www.ogle.com']

i have a Template dome.html:

   {%for i in a %}

   <a href=/link/{{a}}>Click</a><br/>

   {%endfor%}

I have views.py as bellow:

 def someview(request,id):
    do something with input (id)

Now i want to configure my urls.py so that it will communicate with my views.py which take {{a}} as input (id).

url(r'^/link/(url-regex)',someview , name="someview"),

Now i actually want thet url-regex , how i am going to configure it . Kindly help

6
  • May be you could do, {% for i in a %} <a href="{{i}}">Click</a><br/> {% endfor %} Commented Jun 26, 2017 at 4:54
  • Its not clear, what you are implying.. Please explain it a little bit further. Commented Jun 26, 2017 at 4:56
  • i want {{i}} as input to the function someview(request,id) Commented Jun 26, 2017 at 4:57
  • But, {{ i }} is not an id, its just uri. Do you want to go to the uri? Commented Jun 26, 2017 at 5:00
  • i want u url pattern that will send {{i}} to the someview(request,id) fiunction . like url(r'^/link/(urlpattern)', someview, name="someview"), Commented Jun 26, 2017 at 5:00

2 Answers 2

1
# urls.py
from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^link/(?P<id>[0-9]+)/$'', views.someview),
]

Then in your views.py

# views.py

def someview(request,id):
    do something with input (id)

This is a very common pattern that is explained thoroughly by the official docs, so I recommend reviewing that.

Sign up to request clarification or add additional context in comments.

6 Comments

( localhost/link/https://google.com ) this is what i am expecting . what will be the url regex to configure it . i dont want id (which is a numerical value )
that's not a valid URL, you can't pass that as an argument. are the links like http://google.com stored in your database?
No its not stored , thats the problem
you can encode your url using django.utils.http.urlencode and decode it at the view level, but shit may break in many different ways and can be a security concern. I can't imagine what your use case is that warrants accepting random URLs from users and passing them to a view... I think you might be too concerned about the solution for what you think is your problem that you're missing the forest for the trees, also called the XY problem
yes , after adding this urls to database , i can get the (id). But it will involve my huge db space . Is there any way so that i can get the solution without involving the database
|
1

So Finally i have got the ans :

You have to save the the Urls in database as and yo have to point out the id

    a.append(link.objects.get(links=id1))

Then

      {%for i in a %}

    <a href=/link/{{a}}>Click</a><br/>   

     {%endfor%}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.