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.
Related
I am using Django-Filer in my admin on a Django web project which is hosted in PythonAnywhere. I have gone through the installation instructions but I am having trouble accessing the canonical urls the Filer makes for each file. It seems I am being directed to an extended url that Filer.urls is not recognizing (the non-canonical part starts at /filer-public/; this is a directory that is being created and storing my files on the separate PythonAnywhere file directory).
Is there an error in my urls syntax? An error in my views.canonical? I am unsure of why plugging in the exact canonical url redirects me to this extended version of the url.
Python: 3.7
Django: 2.2
Canonical URL:
/filer/sharing/1560887480/39/
ERROR / DEBUGGING SCREEN
Page not found (404)
Request Method: GET
Request URL: http://www.mywebsite.com/filer/sharing/1560887480/39/filer_public/36/ea/36ea58a8-f59c-41ad-9d1f-00a976603eb1/big1.jpg
Using the URLconf defined in mywebsitesite.urls, Django tried these URL patterns, in this order:
admin/
^filer/ sharing/(?P<uploaded_at>[0-9]+)/(?P<file_id>[0-9]+)/$ [name='canonical']
The current path, filer/sharing/1560887480/39/filer_public/36/ea/36ea58a8-f59c-41ad-9d1f-00a976603eb1/big1.jpg, didn't match any of these.
APP URLS: /mywebsite/.virtualenvs/env/lib/python3.7/site-packages/filer/urls.py
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from django.conf.urls import url
from . import settings as filer_settings
from . import views
urlpatterns = [
url(
filer_settings.FILER_CANONICAL_URL + r'(?P<uploaded_at>[0-9]+)/(?P<file_id>[0-9]+)/$', # flake8: noqa
views.canonical,
name='canonical'
),
]
APP VIEWS: /mywebsite/.virtualenvs/env/lib/python3.7/site-packages/filer/VIEWS.py
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from django.http import Http404
from django.shortcuts import get_object_or_404, redirect
from .models import File
def canonical(request, uploaded_at, file_id):
"""
Redirect to the current url of a public file
"""
filer_file = get_object_or_404(File, pk=file_id, is_public=True)
if (not filer_file.file or int(uploaded_at) != filer_file.canonical_time):
raise Http404('No %s matches the given query.' % File._meta.object_name)
return redirect(filer_file.url)
BASE URLS: /home/mywebsite/mywebsite/urls.py
from django.contrib import admin
from django.urls import include, path
from django.conf.urls import url
from django.views.generic import TemplateView
from quotes.views import Register
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('django.contrib.auth.urls')),
path('', include('pages.urls')),
url(r'^filer/', include('filer.urls')),
]
BASE SETTINGS: /home/mywebsite/mywebsite/settings.py
FILER_CANONICAL_URL = 'sharing/'
I was able to get the canonical url to work by configuring the static and media root in my urls and settings files, as shown below.
urls.py
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import include, path
from django.conf.urls import url
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('django.contrib.auth.urls')),
url(r'^filer/', include('filer.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
settings.py
MEDIA_ROOT = os.environ.get('FILER_MEDIA_ROOT', os.path.join(BASE_DIR, 'media'))
MEDIA_URL = '/home/mywebsite/media/'
I was able to solve this by not including the filer.urls but rather specifying the url pattern directly in my projects urls.py file.
Django 3.1.7
django-filer 2.1.2
/myproject/myproject/urls.py:
...
from filer import views as filer_views
from .settings import FILER_CANONICAL_URL
...
urlpatterns = [
url(r'^filer/'+FILER_CANONICAL_URL+r'/(?P<uploaded_at>[0-9]+)/(?P<file_id>[0-9]+)/$',
filer_views.canonical,
name='canonical'),
...,
]
/myproject/myproject/settings.py
...
FILER_CANONICAL_URL = 'somefolder/'
However I'm still not sure where the extra space was coming from in the first place - the filer urls.py and settings.py look fine as far as I can tell.
This is probably pretty simple but I can't get my head around it. I'm learning Django, have v3.0.4 installed and can't get the URLs from an app to work correctly.
On the project urls.py I have the following:
Project\urls.py:
from django.contrib import admin
from django.urls import path
from django.urls import include
from AppTwo import views
urlpatterns = [
path('', views.index, name='index'),
path('', include('AppTwo.urls')),
path('admin/', admin.site.urls),
]
I've created an app named "AppTwo" and have the following urls.py and views.py in the app:
AppTwo\urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('/help', views.help, name='help'),
]
AppTwo\views.py:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("<em>My Second App</em>")
def help(request):
return HttpResponse("<em>Help Page!!!</em>")
If I browse to http://127.0.0.1:8000/ the index page loads and I see the text "My Second App" as expected. However if I browse to http://127.0.0.1:8000/help I get page not found 404 error.
I can also browse to the admin page just fine. So far this is a stock project, the only other change I made after creating it was to the settings.py file to install the "AppTwo" application. Based on the documentation, this looks like it should work, so what am I doing wrong?
yep, knew it was simple.
Changed
path('/help', views.help, name='help'),
to:
path('help/', views.help, name='help'),
all good now.
I've never worked with Django before. I'm taking over a Django project that was started by another programmer, who is now long gone. There is some magic happening in the code that I do not understand. For instance, in this file:
urls.py
I see this:
from django.conf.urls import url, include
from django.contrib import admin
from django.core.urlresolvers import reverse_lazy
from django.views.generic.base import RedirectView
from django.conf import settings
from core import views as core_views
from sugarlab.search.views import validate_collections, create_document, delete_interest, rename_interest, add_url, my_interests
from sugarlab.search.views import content, score, terms
from django.contrib.auth.views import logout as django_logout
from django.conf.urls.static import static
admin.autodiscover()
urlpatterns = [
url(r'^admin/logout/$', django_logout,
{'next_page': '/'}),
url(r'^admin/', admin.site.urls),
url(r'^accounts/logout/$', django_logout,
{'next_page': '/'}),
url(r'^accounts/', include('allauth.urls')),
url(r'^unsecured/$', core_views.home),
The confusing part is these two lines:
from django.conf import settings
url(r'^accounts/', include('allauth.urls')),
"allauths" is some configuration set inside of this file:
settings/common.py
The data looks like this:
'allauth',
'allauth.account',
'allauth.socialaccount',
'django.contrib.auth',
'django.contrib.sites',
# Social/3rd party Authentication apps
'allauth.socialaccount.providers.linkedin_oauth2',
'captcha'
Somehow this is a URL that actually works:
/accounts/signup/
This file is completely blank:
settings/__init__.py
So I've two questions:
how does "import settings" manage to magically import allauths?
how does /accounts/signup/ map to an actual view? I don't see anything in urls.py, nor in settings, that would make me think that /accounts/signup/ is a valid url.
how does /accounts/signup/ map to an actual view? I don't see anything in urls.py, nor in settings, that would make me think that /accounts/signup/ is a valid url.
url(r'^accounts/', include('allauth.urls')),
there is another urls file inside the app called allauth if it's installed by "pip" you can find it in the following directory "lib/python*/site-package/allauth"
= the python version you are using for example 2.7 or 3.5
ps allauth is a well known 3rd party app you can quick google search django allauth and you'll find it
how does "import settings" manage to magically import allauths?
it doesnt import settings is used for something else for example setting static file url like that
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
i am new to django and web frameworks. As said in the documentation of django 1.11.4, i changed polls/view.py as
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
after that i created polls/urls.py and write code in it as:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
finally changed mysite/urls.py as
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
when i run this project with: python manage.py runserver
it shows error when go to "http://localhost:8000/polls/":
Not Found: /polls/
[09/Aug/2017 12:01:36] "GET /polls/ HTTP/1.1" 404 1947
i add polls to installed apps. What else to do ??
Path error. Problem solved. code should be in "mysite/mysite/urls.py" not in "mysite/urls.py"
Probably you did'nt include the polls app properly in your project. Kindly post that line of code.
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!