I have a Django application where, for some reason, I cannot load my URLs without a trailing "/"
I have other Django applications I've made in practice where this hasn't been an issue, and I don't need one when I go to my Admin page, so I can't figure out what's setup wrong here.
Here's my main URLs file:
urlpatterns = [
path('app/', include('app.urls')),
]
Here's my app's URLs file:
urlpatterns = [
path('subapp/', views.subapp, name='subapp'),
]
And my views file:
def subapp(request):
return render(request, 'app/subapp.html', {'title': 'subapp'})
When I enter my URL as "localhost:8000/app/subapp/" it takes me to the correct page.
However, when I enter my URL as "localhost:8000/app/subapp" I get a 404 with the following debug error message: Directory indexes are not allowed here.
What am I doing wrong?
Please check APPEND_SLASH setting in the settings.py file.
Please also check that link:
django urls without a trailing slash do not redirect
For my specific usecase, it was because I needed to add the following to my settings.py:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
This fixed my issue.
Related
I'm trying to render a basic html/css page in Django and I can't get it to work. It's set up seemingly the same as my index page which does work correctly and the debug explanation from the 404 response seems to show that it's pointing to the right url. Any thoughts on why this isn't working?
*I'm using django 2.1 so I'm using path instead of the old regex url mapping
*The html file exists and is located in templates\base_app\our-story.html
From views.py:
def OurStory(request):
return render(request, 'base_app/our-story.html')
From urls.py:
from base_app import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('our-story', views.OurStory, name='our-story')
]
From settings.py:
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
INSTALLED_APPS = [
(standard django apps)
'base_app'
]
From debug message:
Request Method: GET
Request URL: http://127.0.0.1:8000/our-story.html
Using the URLconf defined in base_app.urls, Django tried these URL
patterns, in this order:
admin/
[name='index']
our-story [name='our-story']
The current path, our-story.html, didn't match any of these.
URL should be - http://127.0.0.1:8000/our-story
We cannot use our-story.html, As we are using framework and already have a route.
Use like this, You will definitely get rid of this error.
I guess path of html template would be only our-story.html If Your project name is base_app
In views.py you should return:
return render(request, template, context)
where
template = 'our-story.html'
and
context = { _your_context_dictionary }
also it is a good practice to use lower case names in the views:
def our_story
I'm using models.FileField like this below.
It's a fantastic django function. Because, it makes user can upload within Django administration page without any code.
So, I clicked Part image url Link. But, I got a error message below.
my urls pattern is below.
urls.py
urlpatterns = [
url(r'^parts/', include('parts.urls', namespace='parts')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.MW_Hello.as_view(), name='Hello'),
]
Do I have to add url mapping in url patterns?
You need to configure your urls and settings for managing the static files
https://docs.djangoproject.com/en/1.8/howto/static-files/
https://docs.djangoproject.com/en/1.8/topics/files/
i have just begun learning Django, and i stick with the principle that the fastest and best way to learn is through practice. I'm on the process of building my first web app, and i would really appreciate your help on the following :
I'm working on getting the front end to show up. But i'm having a hard time understanding the way the URLS work.
I have the following directory :
/myApp
/myApp
/public
/templates
/account
login.html
base.html
settings.py
urls.py
...
/account
urls.py
views.py
I have this on my myApp(the main app) 'urls.py' file
urlpatterns = patterns('',
...
url(r'^$', include(account.urls), name='account'),
)
And inside my account urls.py file, i already have the ff :
...
urlpatterns = patterns(
'account.views',
url(r'^$', 'login_user', name='login'),
)
I already defined following on the account views.py file :
def login_user(request):
return render(request, 'account/login.html')
So i guess the request should render my login.html file.
But i get the error that,
NameError at /
name 'account' is not defined
Therefore, i've figured that there must be something wrong with my settings.py file, right?
So here it is if it serves some purpose (just the important stuffs) :
...
BASE_DIR = os.path.join(os.path.dirname(__file__), '.')
...
ROOT_URLCONF = 'myApp.urls'
WSGI_APPLICATION = 'myApp.wsgi.application'
TEMPLATE_DIRS = [
os.path.join(BASE_DIR,'templates'),
]
STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATIC_URL = "/static/"
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'public'),
]
Right now, i really need to at least get the front end working. I hope the details i gave you gives you an idea of how i am organizing my file right now.
Additional note : I want to just create one template directory for the whole app. And as you can see on the structure, the template folder is inside the main app. How do i configure it inside the settings such that the apps use the main template folder?
You should put the account.urls in quotes. Also remove the $ sign from the regex:
url(r'^', include('account.urls')),
And in the account/urls.py file you should correct the base module name from oauth to the acount (your view is account.views.login_user, not the oauth.views.login_user):
urlpatterns = patterns('account.views',
....
)
I am using Django 1.5.4 and I have a project with the following structure:
django_site
-django_site
-books # the app
-media
-images
-books
-authors
-static
-images
-templates
This is the necessary code:
# settings.py
MEDIA_ROOT = '/root/django_site/books/media/'
MEDIA_URL = '/media/'
# models.py
class Book(models.Model):
image = models.ImageField(upload_to="images/books/")
# urls.py
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^books/$', 'books.views.get_all_books'),
)
My problem is that, after adding the image via the admin site, if I click on the image link below the upload button a 404 page shows up, complaining the path does not exist.
The request URL is http://127.0.0.1:8000/media/books/media/images/book/out_of_control_1.JPG but in fact I want the result to be /root/django_site/books/media/images/out_of_control_1.JPG.
How do I fix that? Looking forward to your responses.
Since you seem to use the developpement server, I think your issue is related to this one : Django MEDIA_URL and MEDIA_ROOT
As explained in the answer, you have to set up a specific URL pattern for media handling when debug is set to True.
You can also have a look at the docs regarding this question.
So I'm trying to get TinyMCE up and running on a simple view function, but the path to the tiny_mce.js file gets screwed up. The file is located at /Users/home/Django/tinyMCE/media/js/tiny_mce/tiny_mce.js. I believe that /media/js/tiny_mce/tiny_mce.js is correct, as the development server has no access to files above the root of the Django project folder. In the rendered page, it says in the debugger that the javascript file was not found. This is because it was trying to look through /js/tiny_mce/tiny_mce.js without addressing the /media/ part of the pathname.
Anyway, here's the script snippet for the javascript in a template named 'simple.html'. <script type="text/javascript" src="{{ MEDIA_URL }}/js/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
mode : "textareas",
theme : "simple"
});
</script>
And this is what the vital parts of my settings is like.
MEDIA_ROOT = os.path.join(_base, 'media')
MEDIA_URL = 'http://127.0.0.1:8000/media/'
ADMIN_MEDIA_PREFIX = '/media/'
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'tinymce',
)
It looks like your are using the debug server (your url is http://127.0.0.1:8000/...) . Did you install the static serve in your urls.py?
if settings.DEBUG:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes':True}),
)
The 'show_indexes':True options make possible to browse your media. Go to your medias root http://127.0.0.1:8000/media/ and see it there is something
You can either pass it to the template manually as others have suggested or ensure that you are using a RequestContext instead of plain Context. RequestContext will automatically populate the context with certain variables including MEDIA_URL and other media-related ones.
Example from docs:
def some_view(request):
# ...
return render_to_response('my_template.html',
my_data_dictionary,
context_instance=RequestContext(request))
It looks like ars has answered your real question… But you'll run into another problem: MEDIA_URL must be different from ADMIN_MEDIA_PREFIX. If they aren't, the ADMIN_MEDIA_PREFIX will take precedence. I usually fix this by changing ADMIN_MEDIA_PREFIX to /admin-media/.
Do you have the media context processor in your settings?
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.media',
)
You might also try putting the following in your settings to see if the debug messages reveal anything:
TEMPLATE_DEBUG = True
Please read the official Django DOC carefully and you will find the most fit answer.
The best and easist way to solve this is like below.
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
The related url is: https://docs.djangoproject.com/en/1.5/howto/static-files/#serving-files-uploaded-by-a-user
Thanks a lot for your help everyone. I was able to solve it as
settings.py -->
MEDIA_ROOT = '/home/patrick/Documents/code/projects/test/media/'
MEDIA_URL = 'http://localhost:8000/media/'
urls.py -->
(r'^media/(?P<path>,*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),
index.html -->
img src="/media/images/logo.jpg"
Again, thanks a lot, I was going crazy trying to find out how to do all of this. This solution worked for me on a Django version 1.2.5 Development server running Ubuntu 10.04.
I had a similar problem,
I have a one-page-app and Apparently I had a newbie mistake that made django miss my media path i had this:
url(r'^', home, name="home"),
instead of this:
url(r'^$', home, name="home"),
the missing $ made my url pattern miss my media url
I was also having the same problem, this is how I got it done.
If you are using a FileField in your model class instead of MEDIA_URL use .url member to access the url of your file.
For eg: something like this
href = "{{ some_object.file_name.url }}"
here file_name is the FileField in model class.