0

I'm trying to write URL paths according to data inside Django database (models). Below, my URL.py file:

from . import views
from django.urls import path    
from .models import Seller

sellers = Seller.objects.all
    
app_name = 'root'
urlpatterns = [
    path('', views.index, name='index'),

    for seller in sellers:
      path(seller.name, views.seller, name='sellers'),    
]

I know the 'for-loop' inside the urlpatterns list is completely wrong.

Any idea?

1 Answer 1

1

When creating URLs, you don't have to create a specific url for every object.

In urls.py:

from . import views
from django.urls import path    
    
app_name = 'root'
urlpatterns = [
    path('', views.index, name='index'),
    path('<str:seller_name>/', views.your_function, name="seller_page")

]

In views.py

from .models import Seller

def your_function(request, seller_name):

    # Do something with the object

Happy coding!

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

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.