can not import patterns in django 1.11.4 - python

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.

Related

How can i do my url flexible in Django 1.11?

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.

Django urlpatterns issue

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/

Django: Page Not Found

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..

Django url app namespacing conflicts

I have a Django site with two apps. One is an api and the other is a frontend. For some reason the urls for the frontend are resolving to the api...
game_server/games/urls.py:
from django.conf.urls import patterns, url
from api import views
urlpatterns = patterns('',
url(r'^$', views.index, name = 'index'),
url(r'^tictactoe/$', views.tictactoe, name = 'tictactoe'),
)
game_server/api/urls.py:
from django.conf.urls import patterns, url
from api import views
urlpatterns = patterns('',
url(r'^$', views.index, name = 'index'),
url(r'^tictactoe/$', views.tictactoe, name = 'tictactoe'),
)
game_server/game_server/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^games/', include('games.urls', namespace="games", app_name="games")),
url(r'^admin/', include(admin.site.urls)),
url(r'^api/', include('api.urls', namespace="api", app_name="api"))
)
But whenever I visit 127.0.0.1:8000/games/tictactoe, it gives me the tictactoe view from game_server/api/views.py (and similarly for the index)
I'm sure it's something obvious, but I think I followed the process in the Django polls app pretty much identically...
In games/urls.py you have:
from api import views
You want to import the games views instead:
from games import views

Django - URL routing issues (cannot import name 'urls')

I am following the Django tutorial on https://docs.djangoproject.com/en/1.7/intro/tutorial03/, and am trying to get the index view to show up. I've tried the code specified on the page verbatim but keep on getting errors.
polls/urls.py:
from django.conf.urls import patterns, urls
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
mysite/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
and finally, the index method in views.py:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("<h1>Hello world!</h1>");
I'm not sure what I'm doing wrong. I keep getting an error that says "cannot import name 'urls'." any help would be appreciated!
The problem is in your import statement - there is no urls function in django.conf.urls package.
Replace:
from django.conf.urls import patterns, urls
with:
from django.conf.urls import patterns, url

Categories

Resources