Django url app namespacing conflicts - python

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

Related

can not import patterns in django 1.11.4

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.

How to configure Django views and urls to render specific templates

When I bring up 127.0.0.1:8000, the current page that show up is something.html template.
I would need to make it appear index.html at first launch, then when I click on other parts,it should go to 127.0.0.1:8000/something.html (or 127.0.0.1:8000/myapp/something.html).
What would be the structure to achieve this?
I frequently get error message : The current URL didn't match any of these.
Currently, my structure is
project
---myapp
---admin.py
---models.py
---url.py
---views.py
---static
---templates
---myapp
---html files
---mysite
---settings.py
--- url.py
under my settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
under mysettings/url.py
urlpatterns = [
# Examples:
url(r'^$', 'mysite.views.home', name='home'),
url(r'^myapp/', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
]
mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls import patterns
from django.views.generic import TemplateView
urlpatterns = [
# Examples:
url(r'^', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
]
admin.site.site_header = 'Admin'
myapp/url.py
from . import views
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
urlpatterns = patterns('',
#brings up something.html when we comment it out. No module found if included.
#url(r'^$', views.home, name='home'),
url(r'^$', views.post_list, name='post_list'),
)
Include urls of your myapp app in mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
urlpatterns = [
url(r'^', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
]
Now, all urls, starting by 127.0.0.1:8000, will check if there is a view that can handle the request. I recommend you to read this to understand how Django URL dispatcher works : [Django documentation - URL Dispatcher] (https://docs.djangoproject.com/en/1.8/topics/http/urls/).
2. Add new route in your myapp.urls:
from django.conf.urls import url, patterns
from . import views
urlpatterns = patterns('',
url(r'^$', views.home, name='home'),
url(r'^something$', views.something, name='something'),
url(r'^posts$', views.post_list, name='post_list'),
)
Now :
127.0.0.1:8000/ will executed code of views.home
127.0.0.1:8000/something will executed code of views.something
127.0.0.1:8000/posts will executed code of views.post_list
Let's define these view now
3: Define the views in myapp.views :
from django.shortcuts import render
def home(request):
"""
Return home page
"""
return render(request, 'myapp/home.html')
def something(request):
"""
Return something page
"""
return render(request, 'myapp/something.html')
def post_list(request):
"""
Return something page
"""
# do what you want
Add your templates in myapp/templates/myapp/. Add home.html and something.html.
Now forget, the `.html
You create a url (with attached view to it).
In the view you render any html page you want.
If you use function based views your 'mysite.views.home' may look like:
def home(request):
...
return render(request, 'path/to/index.html')
and so on.
It's the basics so I won't talk about this much. You can find a good tutorial about mapping urls to views there.

Page not found in Django

I'm following this tutorial to learn Django. I'm a total starter.
My urls.py file has following code
from django.conf.urls import include, url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
]
My views.py file is
from django.shortcuts import render
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
When i try to access th url http://127.0.0.1:8000/polls/ in my system it gives a page not found message. What am i doing wrong?
Is it a problem with the version difference?
Here is the screenshot of the error
This error comes if your urls.py file in polls directory does not have this pattern. Create a file urls.py your polls directory and add following url pattern
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
# ex: /polls/
url(r'^$', views.index, name='index'),
)
Ok, so if you want to use /polls/ with this urls,
your polls.urls should look similar to this ( for django1.8):
from django.conf.urls import url
from polls.views import BasePollView, AnotherPollView
urlpatterns = [
url(r'^$', BasePollView.as_view()),
url(r'^another-url/$', AnotherPollView.as_view())
]
/polls/ -> BasePollView
/polls/another-url/ -> AnotherPollView

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

urls.py in django throws "No module found"

I have a question about urls.py in Django. I am building a blog from scratch as a way of learning Django myself. In the main urls.py file, I have specified the include path to my app's urls.py.
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^', include('myblog.urls')),
)
In the app (called myblog), the urls.py reads as follows:
from django.conf.urls.defaults import *
from models import blogmodel
from django.contrib import admin
urlpatterns = patterns('',
(r'^login/', include(admin.site.urls)),
(r'^$', include('myblog.views.getLatest')),
)
where getLAtest is the function in my views.py. The error says No module named getLatest
Here's my views.py,
from django.shortcuts import render_to_response
from myblog.models import blogdb
def getLatest(request):
post = blogdb.objects.all()
sorted_post = post.order_by('-served_date')
return render_to_response('blogs.html', {'posts':sorted_post})
Any help is appreciated. Thanks in advance..
You are using the wrong directive; include() is used to include another package; Django will look for a urls.py within the package myblog.views.getLatest when you use that directive.
You want to name the view itself instead:
urlpatterns = patterns('',
(r'^login/', include(admin.site.urls)),
(r'^$', 'myblog.views.getLatest'),
)
Note: no include() is being used.
Try updating this:
urlpatterns = patterns('',
(r'^login/', include(admin.site.urls)),
(r'^$', include('myblog.views.getLatest')),
)
to this:
urlpatterns = patterns('',
(r'^getLatest/$', 'myblog.views.getLatest'),
)
include is meant to read in another urls.py file, where you you are wanting to execute a specific view function.

Categories

Resources