R
R
Rassul Nassyrov2020-09-11 10:05:35
Django
Rassul Nassyrov, 2020-09-11 10:05:35

How to create a custom /username binding for each object in the database?

Tell me how to create for each object its own url binding, for example 127.0.0.1:800/Rassul. Tried like this but only creates one page : File urls.py in applications:

for i in account.objects.all():
if i.account_login == i.account_login:
    userlogin = i.account_login
    print(userlogin)
    urlpatterns = [
        path('', views.main, name = 'main'),
        path('forgot', views.forgot, name = 'forgot'),
        path('addaccount', views.addaccount, name = 'addaccount'),
        path(userlogin, views.profile, name = userlogin)
    ]


views.py file:

def profile(request):
for a in account.objects.all():
    return render(request, 'index.html')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
gimntut, 2020-09-11
@nssr02

No need to create a url for each user. It is enough to create a universal url using regular expressions
. In your case, there will be something like

path('bio/<username>/', views.bio, name='bio')
# или 
re_path(r'^bio/(?P<username>\w+)/$', views.bio, name='bio')

More details here: https://djbook.ru/rel3.0/ref/urls.html and https://djbook.ru/rel3.0/topics/http/urls.html#usi...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question