I am newbie of django user.I trying to learn django webframework. because of that I meet interesting error and I couldn't solve it. I want save image to sqlite.When I upload an image it's working but when I try to display the picture I meet an error. I tried other answers but its didn't work.I am waiting your help.Thank you
error
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/info/airimage/1/media/Ataturk_Havalimani_2.jpg/
airimage object with primary key u'1/media/Ataturk_Havalimani_2.jpg' does not exist.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
my settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
SECRET_KEY = 'amln51jyuo*wj2#g6k3vdd^#2)&84i#1#n2xvgx7hh#bpf7(l!'
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = [] .......
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'tav.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
and my content of folder
>tav
>info
__init__.py
admin.py
models.py
tests.py
views.py
>media
>tav
__init__.py
settings.py
urls.py
wsgi.py
db.sqlite3
manage.py
model for airimage
class airimage(models.Model):
stuff_image = models.FileField(upload_to="media/")
airno=models.ForeignKey(airandoto)
def __unicode__(Self):
return self.airno
class Meta:
verbose_name_plural="AirImage"
You're attempting to access the image incorrectly. Can you post the model for airimage? The primary key for the image in question looks to be 1, but you are appending extra information to the URL which is making Django think that you are looking for the row with the primary key of '1/media/Ataturk_Havalimani_2.jpg', which definitely does not exist.
Related
I migrated my Django project to an Ubuntu distant server. I'm using mod_wsgi in order to runserver and I have some questions about default page.
I'm really new in this domain and I apologize if my question is bad or useless ..
When I want to connect to my Django application, I have to write something like that :
http://172.XX.XX.XXX/Home/login/
If I write just :
http://172.XX.XX.XXX
I get :
Page not found
Using the URLconf defined in Etat_civil.urls, Django tried these URL patterns, in this order:
^admin/
^BirthCertificate/
^Identity/
^Accueil/
^Home/
^captcha/
^Mairie/
The current URL, , didn't match any of these.
My question is :
How I can define redirected url in order to write http://172.XX.XX.XXX in my browser and go directly to http://172.XX.XX.XXX/Home/login/ for example ?
This is urls.py file from my project :
from django.conf.urls import url, include
from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings
from BirthCertificate import views
from Identity import views
from Accueil import views
from log import views
from Mairie import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^BirthCertificate/', include('BirthCertificate.urls')),
url(r'^Identity/', include('Identity.urls')),
url(r'^Accueil/', include('Accueil.urls')),
url(r'^Home/', include('log.urls')),
url(r'^captcha/', include('captcha.urls')),
url(r'^Mairie/', include('Mairie.urls')),
]
Because I just want for example, write my IP adress in order to access to my Django application and not write all the time a complete url.
If you need some files (apache2 files, ...) please tell me which one I have to post there.
Include this in views.py file of your any app:
from django.shortcuts import redirect
def some_view(request):
return redirect('/Home/login/')
Suppose the view is in log app then,
Include this in your urls.py:
from log.views import some_view
urlpatterns = [
url(r'^$', some_view,name='index'),
url(r'^admin/', admin.site.urls),
url(r'^BirthCertificate/', include('BirthCertificate.urls')),
url(r'^Identity/', include('Identity.urls')),
url(r'^Accueil/', include('Accueil.urls')),
url(r'^Home/', include('log.urls')),
url(r'^captcha/', include('captcha.urls')),
url(r'^Mairie/', include('Mairie.urls')),
]
One method is to use RedirectView by making the following changes to your urls.py:
from django.views.generic.base import RedirectView
urlpatterns = [
url(r'^$', RedirectView.as_view(url='/Home'), name='home'),
...
]
You can either do this in Django or Configure from your webserver. Django has redirects app go through https://docs.djangoproject.com/en/1.10/ref/contrib/redirects/ for more info. You just add the source and redirect urls in the redirects section of your django admin.
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'm new in Django and use this tutorial for learning https://docs.djangoproject.com/en/1.6/intro/tutorial03/
Have a little problem with routing in.
View code:
def index(request):
return HttpResponse(r'<h3 style="font-style: bold;">Index</h3>')
URL config code:
1. blog/urls
urlpatterns = patterns('',
url(r'^$', views.index, name='index')
)
2.project/urls
urlpatterns = patterns('',
url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
When i come for 127.0.0.1:8080/index that
Page not found (404)
Using the URLconf defined in les1.urls, Django tried these URL patterns, in this order:
^blog/
^admin/
The current URL, index, didn't match any of these.
Struct of project
blog/
templates
__init__.py
admin.py
models.py
tests.py
urls.py
views.py
les1/
__init__.py
settings.py
urls.py
wsgi.py
db.sqlite3
manage.py
Can't find error:(
What is the actual url you enter in your browser ? is it 127.0.0.1:8000 or 127.0.0.1:8000/index ? From your post, I assume it is the second:
The current URL, index, didn't match any of these.
What if you enter 127.0.0.1:8000/blog ?
index is the name of your route, used only on server-side. It's a shortcut used for quick reversing and displaying of URL:
from django.core.urlresolvers import reverse
print(reverse('index'))
# will output "/blog", which is the actual URL
Remove r from view code.
def index(request):
return HttpResponse('<h3 style="font-style: bold;">Index</h3>')
There are many similar questions posted already, but I've already tried those solutions to no avail. I'm working through a basic Django tutorial, and here is my code:
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'tango_with_django_project.views.home', name='home'),
# url(r'^tango_with_django_project/', include('tango_with_django_project.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
url(r'^rango/', include('rango.urls')), # ADD THIS NEW TUPLE!
)
views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Rango says hello world!")
From the settings.py file
ROOT_URLCONF = 'tango_with_django_project.urls'
Hope you all can help get me started
Let's say I have a Django project called FailBook, with two apps, posts and links. If I look into FailBook/urls.py, I will find something like
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'^posts/', include('posts.urls')), ## Custom url include
url(r'^links/', include('links.urls')), ## Custom url include
)
So then, when you look into the directory structure, you will notice that there are extra two urls.py files
FailBook
|-- posts
|-- models.py
|-- urls.py
|-- views.py
|-- etc.
|-- links
|-- models.py
|-- urls.py
|-- views.py
|-- etc.
# urls.py file in the posts folder
from django.conf.urls import patterns, include, url
from .views import PostListView, PostDetailView
urlpatterns = patterns('',
url(r'^posts/', PostListView.as_view()),
url(r'^posts/(?P<post_id>\d+)', PostDetailView.as_view()),
)
# where both views are class based views, hence the as_view function call
I know this was already solved, but the solutions provided did not help me. When I had this error it was as simple as checking all of the directories that should have had urls.py files.What I discovered was that the urls.py had not been added to the SVN repository that our Django app was pulled from.
I recommend looking in the projectname->projectname->urls.py for all references to app specific urls, and verifying that the urls.py file exists for each of them.
I had this issue while doing a Pluralsight Django tutorial. Two things I noticed:
1) I was using Django 2.0, and the command url() from Django 1.11 has been replaced with re_path() in 2.0, obtained by importing as follows:
from django.urls import path, include, re_path
This replaces,
from django.conf.urls import url, include
see this: Django 2.0 release notes
and,
2) I accidentally called the file to be imported from the subfolder, url.py, not urls.py
e.g. rango/url.py not rango/urls.py.
This was probably the main issue, and all flowed smoothly after that fix.
I'm setting up the django admin on a project.
I've created admin.py files in each of my apps (just as I have on previous projects). However the admin.py modules are not being loaded. My models aren't registered and I can't break into the code with pydevd.settrace().
If I move my admin code to the end of models.py everything works as expected, the models are registered with admin, and the code runs (I can step through it with pydevd.settrace()).
So this fails -
my_project_app
__init__.py
cart
__init__.py
models.py
admin.py
But when I add my code to the end of the models.py file everything runs fine -
from django.contrib import admin
class CartAdmin(admin.ModelAdmin):
pass
admin.site.register(Cart, CartAdmin)
Obviously I'm going to be configuring the admin so I don't want everything in one module. How do I get my admin.py file working? And why have they stopped working (this is the first time I've used django 1.5 - not sure if that's relevant)
Usually you call admin.autodiscover() in your urls.py and it loads everything:
# urls.py
from django.conf.urls import patterns, include
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
)
Since you have to do it before trying to use admin.site.urls, that's the best place to call it.
In your admin.py file you have to import your models
from cart.models import Cart
If still it didnot work then Check if you have included
'django.contrib.admin',
in your installed_apps list of settings file then in urls file include
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
)
run manange.py syncdb
run manage.py runserver
check 127.0.0.1:8000/admin
It should work