I have a project with three applications installed. The first (photologue) is working fine, but I'm having problems with the last two. My urls.py file in the Django site looks like this:
from django.conf.urls.defaults import patterns, include, url
from django.contrib import auth
from django.contrib import admin
from funvisis.users.models import FVISUser
admin.site.register(FVISUser)
admin.autodiscover()
urlpatterns = patterns(
'',
(r'^admin/', include(admin.site.urls)),
(r'^photologue/', include('photologue.urls')),
(r'^inspeccionespuentes/', include('funvisis.bridgeinspections.urls')),
(r'^inspeccionesedificios/', include('funvisis.buildinginspections.urls')),
)
The urls.py file on both of my applications looks like:
from django.conf.urls.defaults import patterns, include, url
from django.conf import settings
from .admin import admin_site
from .views import csv_view
urlpatterns = patterns('',
url(r'^csv/(?P<models_url>\w+)/', csv_view),
(r'', include(admin_site.urls),
)
The problem arise when I try to reach the url "^inspeccionesedificios/", since there is no link to add a new buildinginspection and the link to list all the inspections is formed as "http://127.0.0.1:8000/inspeccionespuentes/buildinginspections/" (note how it starts with "inspeccionespuentes" rather than "inspeccionesedificios").
If I change the order of the patterns in the Django site from:
(r'^inspeccionespuentes/', include('funvisis.bridgeinspections.urls')),
(r'^inspeccionesedificios/', include('funvisis.buildinginspections.urls')),
to:
(r'^inspeccionesedificios/', include('funvisis.buildinginspections.urls')),
(r'^inspeccionespuentes/', include('funvisis.bridgeinspections.urls')),
results in the same behaviour but with the problem in "inspeccionespuentes".
I have recently migrated from Django 1.3 to Django 1.4 and this problem ain't appeared before the migration. Any idea?
Thanks!
Related
Im new to Django but after solving the orginal issues and changing path to re_path in all my URL files Django now starts the server with no issues. The URLs load but all pages are blank except the home page.
`
from django.urls import re_path
from django.conf.urls import include
from django.contrib import admin
#from django.urls import path
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
re_path('admin/', admin.site.urls),
re_path('', include('home.urls')),
re_path("users/", include("django.contrib.auth.urls")),
re_path("users/", include("users.urls")),
re_path("accounts/", include("accounts.urls")),
My console shows no errors so I am unsure what I am doing wrong.
`
The fix was done by updating all url.py files.
1st Remove
from django.conf.urls import url
replace with
from django.urls import path
if any paths start with url simply change to path
for example
url('posts/',views.posts, name = 'posts'),
would become
path('posts/',views.posts, name = 'posts'),
save the files and rerun the server.
[A similar question was asked, but not marked as answered, here. I considered continuing that thread but the website told me I'm only supposed to post an answer, so it seems I have to start a new topic.] I'm trying to follow this tutorial and I'm having problems with the URL mapping. Specifically with the part described as "So best practice is to create an “url.py” per application and to include it in our main projects url.py file". The relevant, I hope, part of the folder structure, which arose by following steps of the tutorial to the letter (if possible; usage of the 'patterns' module was impossible for example) and using Django 1.10 is the following:
myproject/
myapp/
urls.py
views.py
myproject/
urls.py
The myproject/urls.py is as follows:
from django.conf.urls import include, url
from django.contrib import admin
admin.autodiscover()
from myapp.views import hello
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include(myapp.urls)),
]
The myapp/urls.py is as follows:
from django.conf.urls import include, url
urlpatterns = [
url(r'^hello/', myapp.views.hello),
]
The myapp/views.py is as follows:
from django.shortcuts import render
def hello(request):
return render(request, "hello.html", {})
However, running 'python manage.py runserver' results in the following error:
url(r'^myapp/', include(myapp.urls)),
NameError: name 'myapp' is not defined
INSTALLED_APPS in settings.py contains 'myapp'.
I'd be greatful for any tips on how to deal with the NameError! [Or any tips whatsoever that anyone might consider to be helpful!]
You have the NameError because you are referencing myapp in myproject/urls.py but haven't imported it.
The typical approach in Django is to use a string with include, which means that the import is not required.
url(r'^myapp/', include('myapp.urls')),
Since you have move the hello URL pattern into myapp/urls.py, you can remove from myapp.views import hello from myproject/urls.py.
Once you've made that change, you will get another NameError in myapp/urls.py. In this case, a common approach is to use a relative import for the app's views.
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^hello/$', views.hello),
]
Make sure you have imported following modules to urls.py.
from django.conf.urls import url
from django.contrib import admin
in django 2.0
use these
from django.contrib import admin
from django.urls import path
from first_app import views
urlpatterns = [
path('',views.index, name="index"),
path('admin/', admin.site.urls),
]
your app URL has to be a string
so, here is how the code should look like.
from django.conf.urls import include, url
from django.contrib import admin
admin.autodiscover()
from myapp.views import hello
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myapp.urls')),
]
also, note that from python 2 upward the regular expression is not needed.
change URL to path
from django.conf.URLs import include path
from Django.contrib import admin
admin.autodiscover()
from myapp.views import hello
urlpatterns = [
path('^admin/', include(admin.site.urls)),
path('^myapp/', include('myapp.urls')),
]
In Django 2.1.7 here is the default urls .py file
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
so we need to add this line as well
from django.conf.urls import url
I have followed #Alasdair answers
You have the NameError because you are referencing myapp in myproject/urls.py but haven't imported it.
The typical approach in Django is to use a string with include, which
means that the import is not required.
Unfortunately, it didn't work out(I still got the name X is not defined error). Here is how I do it.
from django.contrib import admin
from django.urls import include
from django.conf.urls import url
from article import urls as article_users
from article import urls as user_urls
urlpatterns = [
path('admin/', admin.site.urls),
path('api/article/', include(article_users)),
path('api/user/', include(user_urls)),
]
Before using the URL command be sure to first import the url from the module Urls. Then try using the runserver.
from django.conf.urls import url
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, patterns, include
from django.contrib import admin
from django.views.generic import TemplateView
from collection import *
#from collection.views import index,thing_detail,edit_thing
urlpatterns = [
url(r'^$', views.index, name='home'),
url(r'^about/$',TemplateView.as_view(template_name='about.html'),name='about'),
url(r'^contact/$',TemplateView.as_view(template_name='contact.html'),name='contact'),
url(r'^things/(?P<slug>[-\w]+)/$', 'views.thing_detail' ,name='thing_detail'),
url(r'^things/(?P<slug>[-\w]+)/edit/$', 'views.edit_thing',name='edit_thing'),
url(r'^admin/', include(admin.site.urls)),
]
After running the server there is an error "NameError: name 'views' is not defined"
Any help ??
You aren't importing your own views.
Try adding this to your urls.py:
from . import views
Or if you are importing them from a specific app, try replacing . with the app name
First thing I notice is the import *, realize that this will/can cause confusion for other Developers reading your scripts. Python has a methodology that insists that explicit is better than implicit. Which in this senario means you should be explicit about what you are importing.
from django.conf.urls import url, patterns, include
from django.contrib import admin
from django.views.generic import TemplateView
from collection import views as collection_views
urlpatterns = [
# Function Based Views
url(r'^$', collection_views.index, name='home'),
url(r'^things/(?P<slug>[-\w]+)/$', collection_views.thing_detail ,name='thing_detail'),
url(r'^things/(?P<slug>[-\w]+)/edit/$', collection_views.edit_thing,name='edit_thing'),
# Class Based Views
url(r'^about/$',TemplateView.as_view(template_name='about.html'),name='about'),
url(r'^contact/$',TemplateView.as_view(template_name='contact.html'),name='contact'),
# Admin
url(r'^admin/', include(admin.site.urls)),
]
Here instead of importing everything from collection I'm importing just your views and assigning them to a variable. Then using that variable in the URL definitions.
Be sure to import your views by
specifying its location and the methods inside the view to be imported on your urls.py.
from . collection import *
(line above means from current location find collection.py and import everything on it)
Happy coding!
I've installed Django 1.6 and setup Gunicorn with Python 3.3. Everything works fine together as far as I can tell.
I'm trying to get a URL to look like this: web.com/piña. I've started an app with python3 manage.py startapp pina and created a super basic view.
# pina/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse('¡Hola mundo! Esta es mi primera vista.')
I've also added a new URL entry like this.
# django_project/urls.py
from django.conf.urls import patterns, include, url
from pina import views
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^pi%C3%B1a/', views.index, name='index'),
)
But when I visit web.com/piña/, I get a 404 page.
The /admin/ URL works just fine. If I change r'^pi%C3%B1a/' to just r'^pina/', that also works—but that's a different word entirely.
I'm writing my third django app using the tutorial https://docs.djangoproject.com/en/dev/intro/tutorial03/ and i'm having trouble mapping an URLconf to another URLconf .
I am going to try and explain my problem as clear as I can .
I have an mysite directory and app directory which I called myapp inside the mysite directory.I am trying to point URLconf of the mysite directory to the URLconf of the myapp directory. Once it's pointed it will execute an 'hello' from the myapp view.py.
The path are :
home/superman/mysite
and my myapp path is
home/superman/mysite/myapp
My urls.conf for mysite is
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^polls/',include('mysite.myapp.urls')),
)
My view.py inside myapp are
from django.http import HttpResponse
def index(request):
return HttpResponse("hello")# Create your views here.
My urls.py inside myapp are
django.conf.urls import patterns,url
from mysite.myapp.views import index
urlpatterns=patterns('',
url(r'^$',index, name'index')
)
Thank you for helping me
I think you have already done that with line
url(r'^polls/',include('mysite.myapp.urls')),
in your mysite/urls.py. The index view will be visible on url http://yourserver/polls/
I'm hoping you have fixed python path appropriate so that imports like from mysite.myapp.views import index works well. If not you can fix them as from myapp.views import index.
And then update the urls.py as
url(r'^polls/',include('myapp.urls')),
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^polls/',include('myapp.urls', namespace="myapp")),
)
------------------------------------------------------------
django.conf.urls import patterns, url
urlpatterns=patterns('myapp.views',
url(r'^$','index', name='index')
)