In my project with django I should do a site with flexible url. For instance:
mysite.com/register/kj-1k-32-mk
When someone write some other things after register/ I want to redirect them to my site. How can I redirect them. What should my urls.py looks like?
urls.py
from django.conf.urls import url
from django.contrib import admin
from olvapp import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^register/(?P<pk>\d+)',views.guaform,name='custform'),
]
When I do like above, the part after register/ can't begin with a letter.
That's because of the \d+ pattern which only allows a sequence of digits. If you want a slug-like sequence, you can use:
from django.conf.urls import url
from django.contrib import admin
from olvapp import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^register/(?P<pk>[-\w]+)',views.guaform,name='custform'),
]
Note: django-1.11 is no longer supportedĀ [Django-doc] since April 2020, you should consider upgrading the Django version.
Related
I have a problem with Django urls:
main urls.py
from django.contrib import admin
from django.urls import include, re_path, path
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^', include('tutorials.urls')),
]
tutorials/urls.py
from django.urls import re_path
from tutorials import views
urlpatterns = [
re_path(r'^api/tutorials$', views.tutorial_list),
re_path(r'^api/tutorials/(?P<pk>[0-9]+)$', views.tutorial_detail),
re_path(r'^api/tutorials/published$', views.tutorial_list_published)
]
when i go to check API via browser after python manage.py runserver i get:
Looks like regular expressions are not resolved? Any ideas?
You asked the "http://127.0.0.1:8080/api" URL.
Your urlpatterns does not contains any path which could match with this url.
I don't know where you want to go with this url, but if you add
path('api', views.tutorial_list),
In your urlpatterns list, you will go to tutorial_list view for example.
You don't need to use re_path for path without regular expression. just use path function.
I am working from the Python Crash Course from no starch by Eric Matthes. The book is working with Python 2.0 and I am trying to essentially convert it to work with Python 3.0. I am having an issue with include(). The error I am receiving in command prompt is:
django.core.exceptions.ImproperlyConfigured: passing a 3-tuple to
include() is not supported. Pass a 2-tuple containing the list of
patterns and app_name, and provide the namespace argument to include()
instead.
Here is my code:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'', include('learning_logs.urls', namespace='learning_logs')),
]
First of all, you should always mention your Django version when you are asking questions.
As per your code, this is the old way of doing things. If I'm not wrong, starting from Django 2 you have to do this in a little different way.
There are 2 ways to solve it.
First way:
change your code to this:
from django.conf.urls import include, url
from django.contrib import admin
learning_logs_patterns = ([
url(...),
], 'learning_logs')
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^polls/', include(learning_logs_patterns, namespace='learning_logs')),
]
Second way:
in mysite.urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'', include('learning_logs.urls')),
]
in learning_logs.urls.py
app_name = 'polls'
urlpatterns = [...]
For Details see the documentation : https://docs.djangoproject.com/en/3.1/releases/1.9/#passing-a-3-tuple-or-an-app-name-to-include
A little suggestion: Starting from Django 2 there is a better way for declaring urls.It's called path(). See documentation for details: https://docs.djangoproject.com/en/3.1/ref/urls/
I am doing django course from udemy, i did one experiment. Below is my folder structure
Project
appTwo
urls.py
ProTwo
urls.py
appTwo/urls.py
from django.conf.urls import url
from appTwo import views
urlpatterns = [
url(r'^$',views.help,name='help'),
url(r'^$',views.users,name='users'),
]
ProTwo/urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import url,include
from appTwo import views
urlpatterns = [
url(r'^$',views.index,name='index'),
url(r'^help/',include('appTwo.urls')),
url(r'^users/',include('appTwo.urls')),
path('admin/', admin.site.urls),
]
Now when i try to open the page users by http://127.0.0.1:8000/users it opens the page help.html. For http://127.0.0.1:8000/help it opens help page. When I comment the first entry in urlpatterns in urls.py it opens the users page even if i try to open help page. Can anyone please guide me what wrong I am doing or its working as expected.
you need to use different patterns for each View:
urlpatterns = [
url(r'^help$', views.help, name='help'),
url(r'^users$', views.users, name='users'),
]
the name attribute is only useful for the concept of reversing, maybe further in your course?
also, the r'' strings in python are regular expressions, you might want to learn more about them.
I got the answer for this. Below two files need to be changed
appTwo/urls.py
from django.conf.urls import url
from appTwo import views
urlpatterns = [
url(r'^help$',views.help,name='help'),
url(r'^users$',views.users,name='users'),
]
ProTwo/urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import url,include
from appTwo import views
urlpatterns = [
url(r'^$',views.index,name='index'),
url(r'^appTwo/',include('appTwo.urls')),
path('admin/', admin.site.urls),
]
Explanation:
When I type in browser "basic url" i mean the address here it is http://127.0.0.1:8000/, it will go to ProTwo/urls.py which is the project folder. This will open the index page as per line url(r'^$',views.index,name='index'). If you need two configure two different page, give url(r'^appTwo/',include('appTwo.urls')), in the ProTwo/urls.py. This will call appTwo/urls.py. Now for help type http://127.0.0.1:8000/appTwo/help and for users type http://127.0.0.1:8000/appTwo/users.
My project urls.py is as follows:
from django.conf.urls import include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myapp.urls')),
]
My app urls.py is as follows:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('', url(r'^hello/', 'myapp.views.hello', name = 'hello'),)
Now , as soon as I try to run it , it gives me the following error:
from django.conf.urls import patterns, include, url
ImportError: cannot import name 'patterns'
Django doesn't require you to use patterns anymore, you can just make urlpatterns a list of urls
urlpatterns = [url(r'^hello/', 'myapp.views.hello', name = 'hello'),]
Django 1.11 loads that Python module and looks for the variable urlpatterns. This should be a Python list of django.conf.urls.url() instances.
And it runs through each URL pattern, in order, and stops at the first one that matches the requested URL
Dont require patterns more.
urlpatterns should be a Python list of url() instances.
I'm getting a 404 from Django and none of the previous posts on the subject (of which there are many) seem to have helped.
views.py
from django.views.generic.detail import DetailView, SingleObjectMixin
from app.models import MyModel
class MyDetails(DetailView, SingleObjectMixin):
template_name = "app/my_view.html"
model = MyModel
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from app.views import MainList, post_form_upload, MyDetails
urlpatterns = [
url(r'^$', MainList.as_view(), name="main_list"),
url(r'^add_something$', post_form_upload, name="add_something"),
url(r'^my_details/(?P<pk>\d+)$', MyDetails.as_view(), name="my_details"),
]
app/urls.py
from django.conf.urls import url
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
(r'^$', include('app.urls')),
url(r'^admin/', include(admin.site.urls)),
]
when I enter the URL: http://localhost:8000/my_details, I get the following error:
Using the URLconf defined in app.urls, Django tried these URL patterns, in this order:
^$ [name='main_list']
^add_something$ [name='add_something']
^my_details/(?P<pk>\d+)$ [name='my_details']
The current URL, my_details, didn't match any of these.
The other two URLs (/ and /add_something) work fine.
First of all, not sure how you are not running into this issue, but in your app/urls.py
(r'^$', include('app.urls')),
should be
(r'^/', include('app.urls')),
$ indicates the end of regex pattern, and anything inside include() would not be included.
Secondly, None of your URL patterns match my_details/ They only match my_details/<id>
A little more on the documentation of URL regex, etc..