I am learning Django and am having an issue with some simple testing and passing URL parameters. Here is my urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('custnames', views.custnames, name='custnames'),
path('custdetail/<int:cust_id>/', views.cust_detail, name='cust_detail'),
]
And here is my views.py
def cust_detail(request, cust_id):
return HttpResponse('<p>Cust Detail View with cust_id {cust_id}</p>')
When I put this for my URL in my browser: http://localhost:8000/custdetail/1/
My output is:
Cust Detail View with cust_id {cust_id}
The "home" and "custnames" sections seem to work fine.
Any ideas on what I'm doing wrong here?
~Ed
Thanks to "minglyu" and "Melvyn". Adding the "f" worked.
Related
This is how I defined urls.py file of my app
router = DefaultRouter()
router.register('hello-viewset', views.HelloViewSet, base_name='hello-viewset')
router.register('profiles', views.UserProfileViewSet)
router.register('schema', views.SchemaViewSet)
router.register('creddefination', views.CredDefViewSet)
router.register('overalltable', views.OverallViewSet)
urlpatterns = [
path('', include(router.urls)),
]
urls.py of Project:
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('DIAPI.urls')),
]
I am not getting correct address for creddefination. But when i manually go to http://127.0.0.1:7000/api/creddefination/ it is working. It is just not displaying correctly. What might be reason for this
I guess views.CredDefViewSet and views.OverallViewSet are using the same model.
If that's true, then the default register's basename will be named after that model and used as name in a call to Django's reverse url construction. Since the API Root view will be trying to resolve both views with the same name, it'll lead to the same url.
Workaround is to explicitly add a basename to one of the view:
router.register('creddefination', views.CredDefViewSet, basename='creddeef')
Django is throwing me a NoReverseMatch function but I cannot find the reason.
I did check whether I use the reverse function correctly and it seems it does. I also think it is not the custom converters I created because it throws a NoReverseMatch exception.
my forms.py:
#some link creation function
link = reverse("activate",
kwargs={"key":key, "usermail":self.cleaned_data['email']}
)
#sending the link to a user
root urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('accounts.urls')),
path('accounts/', include('django.contrib.auth.urls')),
path('', TemplateView.as_view(template_name='home.html'), name='home'),
]
accounts.urls.py
path('activate/<key:user_key>/<mail:usermail>',
views.activate,
name="activate"
),
]
I just expect it to create this wonderful piece of a link and I cannot find what I am doing wrong. Maybe I am looking at the wrong place, maybe it is an integration error, I don't know. Any help is much appreciated.
edit: added the root urls.py
You should use namespace in your url include
path('accounts/', include('accounts.urls', namespace='accounts')),
and in your forms.py
link = reverse("accounts:activate",
kwargs={"user_key":key, "usermail":self.cleaned_data['email']}
)
I have the following in my root URLconf module (there's more, but not important, so left out):
urlpatterns = [
re_path(r'^password-reset-redirect-view/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
password_reset_redirect,
name = 'password_reset_confirm'),
path('', include('search.urls')),
path('', include('customer_portal.urls')),
path('rest-auth/', include('rest_auth.urls')),
path('rest-auth/registration/', include('rest_auth.registration.urls')),
Here's the customer_portal.urls:
urlpatterns = [
path('customer/contact/', views.contact),
path('', views.home),
re_path(r"^confirm-email/(?P<key>[-:\w]+)/$", views.email_verification,
name="account_confirm_email"),
]
Here's the rest_auth.registration.urls:
urlpatterns = [
url(r'^$', RegisterView.as_view(), name='rest_register'),
url(r'^verify-email/$', VerifyEmailView.as_view(), name='rest_verify_email'),
url(r'^account-confirm-email/(?P<key>[-:\w]+)/$', TemplateView.as_view(),
name='account_confirm_email'),
]
As you can see both included urls.py urlpatterns have a view named 'account_confirm_email'.
Somewhere in the code this is ran:
url = reverse(
"account_confirm_email",
args=[emailconfirmation.key])
Since customer_portal.urls is included before rest_auth.registration.urls, I expect the route account_confirm_email in customer_portal.urls to be returned by the above reverse method. But instead I get the rest_auth.registration.urls route URL.
Just to be sure I commented out the route in rest_auth.registration.urls, and then I did get the correct URL (customer_portal URL) returned.
It is filled into an email, I check that email and see that I have the wanted url: http://127.0.0.1:8000/confirm-email/......./, instead of: http://127.0.0.1:8000/rest-auth/registration/account-confirm-email/...../
Can anyone tell me why the customer_portal URL isn't the one being reversed in both cases?
Django docs say:
Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL.
Was hoping someone could point me in the right direction with this. I've tried nearly everything I can think of, but I can't seem to get it to work. I've got a set of URLs I'd like to match in Django:
www.something.com/django/tabs/
www.something.com/django/tabs/?id=1
Basically, I want to make it so that when you just visit www.something.com/django/tabs/ it takes you to a splash page where you can browse through stuff. When you visit the second URL however, it takes you to a specific page which you can browse to from the first URL. This page is rendered based on an object in the database, which is why the id number is there. I've tried to account for this in the URL regex, but nothing I try seems to work. They all just take me to the main page.
This is what I have in urls.py within the main site folder:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^tabs/', include("tabs.urls")),
]
and within urls.py in the app's folder:
urlpatterns = [
url(r'\?id=\d+$', tab),
url(r'^$', alltabs)
]
Would anyone be so kind as to point me in the right direction? Thanks in advance!
You are not following the right approach here. Query paramers are used to change the behaviour of the page slightly. Like a added filter, search query etc.
What i would suggest is you have only one view and render different templates based on query parameters in the view.
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^tabs/', alltabs),
]
In your alltab views you can have something like this.
def alltabs(request):
if request.GET.get("id"):
id = request.GET.get("id")
your_object = MyModel.objects.get(id=id)
return render_to_response("tab.html", {"object":your_object})
return render_to_response("alltab.html")
Hope this helps
This is not the preferred 'django way' of defining urls patterns, I would say:-)
In the spirit of django would be something like
www.something.com/django/tabs/
www.something.com/django/tabs/1/
....
www.something.com/django/tabs/4/
and for this you define your url patterns within the app for example this way
tabs/urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
# ex: /tabs/
url(r'^$', views.index, name='index'),
# ex: /tabs/5/
url(r'^(?P<tab_id>[0-9]+)/$', views.detail, name='detail'),
# ex: /tabs/5/results/
url(r'^(?P<tab_id>[0-9]+)/results/$', views.results, name='results'),
]
and something similar in your views
tabs/views.py:
from django.shortcuts import get_object_or_404, render
from tabs.models import Tab
def index(request):
return render(request, 'tabs/index.html')
def detail(request, tab_id):
tab = get_object_or_404(Tab, pk=tab_id)
return render(request, 'tabs/detail.html', {'tab': tab})
...
You can follow this django tutorial for more details:
some question about django 1.10:
I am going to make a website and the home page should be a login page created by django,when i tried to make the admin login page as my site's home page,it failed,can anybody help me ?
my urls.py:
urlpatterns = [ url(r'^$', include(admin.site.urls)), ]
or
urlpatterns = [ url(r'^$', admin.site.urls), ]
both works wrong.
thx
For djanog version 2 & later, the syntax url() has been replaced & simplified with path()
So, if you have django version >=2 , use this:
urlpatterns = [
path('', admin.site.urls),
]
Read the docs for details.
Generally speaking, setting admin page as the main page is not a good idea but still, whatever the use case is, try this:
url(r'^', admin.site.urls),
Tested it on Django 1.9 but I think it'll work on Django 1.10 as well.
Also, don't forget to restart the server after making these changes.