I am learning django, and have ran python manage.py startapp myapp which created the following folder structure:
myapp/
__init__.py
admin.py
models.py
tests.py
views.py
I also added myapp to INSTALLED_APPS in settings,
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
)
then I ran python manage.py migrate and python manage.py createsuperuser to create super user
I have also created views of myapp :
"from django.http import HttpResponse
def hello(request):
text = """<h1>welcome to my app !</h1>"""
return HttpResponse(text)
"
Finally, here's the URL mapping:
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'^hello/', 'myapp.views.hello', name = 'hello'),
)
When ran, it throws an error stating that "myapp" is not defined. And I am not able to access admin page at http://127.0.0.1:8000/admin
How can I solve this error and get my application to work?
Possibly myapp is not included in INSTALLED_APPS in setting.py. I suppose you are following this tutorial.
add this in your url mapping:
from myapp import views
Related
I am new to django and I am doing a coursera course with little applications deployed on pythonanywhere, which has worked well so far. No I am stuck, because does not load at all. Pythonanywhere says that Something went wrong.
This is the error log from pythonanywhere.
Error running WSGI application
ModuleNotFoundError: No module named 'django_extensions'
This is my WSGI file
"""
WSGI config for mysite project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
application = get_wsgi_application()
This is my settings.py file, not the entire one but the installed apps:
DEBUG = True
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'ads.apps.AdsConfig',
# Extensions - installed with pip3 / requirements.txt
'django_extensions',
'crispy_forms',
'rest_framework',
'social_django',
'taggit',
'home.apps.HomeConfig',
]
This is my urls.py, not the entire one, but the crucial part.
import os
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls import url
from django.contrib.auth import views as auth_views
from django.views.static import serve
urlpatterns = [
path('', include('home.urls')), # Change to ads.urls
path('ads/', include('ads.urls')),
path('admin/', admin.site.urls), # Keep
path('accounts/', include('django.contrib.auth.urls')), # Keep
# url(r'^oauth/', include('social_django.urls', namespace='social')), # Keep
]
I checked django version, requirements are installed. I double checked with pip list.
Running python3 manage.py check has no errors. I can do all mirgrations without errors. I don't know anymore where to look for the problem.
I already did 3 or 4 of these mini applications and I did not have this problem. wsgi points to mysite.settings and the settings.py has django_extensions as INSTALLES APPS.
What is the problem? Please help
There are multiple Python versions installed on PythonAnywhere. Make sure that you installed the required module for the same Python environment that your web app is being run by. For example, if your web app is set to be run by Python 3.8, check if you installed the module with pip3.8 django_extensions --user command; if you are using a virtual environment, make sure it's set on the Web page and that you installed the module inside of that venv.
I'm new to Django and I'm following this tutorial: https://www.django-rest-framework.org/tutorial/1-serialization/
When I run the server with python manage.py runserver, it seems to work fine showing me no errors.
However when I visit http://127.0.0.1:8000/snippets/ it gives me the following error:
Using the URLconf defined in tutorial.urls, Django tried these URL patterns, in this order:
admin/
The current path, snippets/, didn’t match any of these.
Here's my urls.py located in the tutorial folder:
from django.urls import path, include
urlpatterns = [
path('', include('snippets.urls')),
]
I don't understand what's going on, how come it only checks admin/ if I'm wiring the root urlconf to snippets.urls?
On a related note, when I modify urls.py and add gibberish the server won't give me an error, however if I were to modify models.py then it'll start complaining, that's why I'm getting the feeling it doesn't even check my urls.py and maybe instead some default one...
Any tips are very appreciated!
EDIT:
Here's my urls.py located in the snippets folder:
from django.urls import path
from snippets import views
urlpatterns = [
path('snippets/', views.snippet_list),
path('snippets/<int:pk>/', views.snippet_detail),
]
In settings.py I've only modified the INSTALLED_APPS part with the two last apps:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'snippets.apps.SnippetsConfig',
]
Are you sure you don't have another instance of Django running?
That's the only thing I can think of since admin isn't even in your current urls
I am trying to setup url dispatcher in my project. I have project with follwing structure
forecast
|___ forecast
|_______ __init__.py
|_______ settings.py
|_______ urls.py
|_______ wsgi.py
|____authorization
|_______ apps.py
|_______ urls.py
|_______ models.py
|_______ views.py
|___ templates
|_______ registration
|____________ login.html
In my forecast/urls.py i put logic of
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('main.urls')),
path('auth',
include('authorization.urls')),
]
The main point for my question is path('auth',include('authorization.urls')) i include urls.py from authorization/ and in this urls.py i want to put all logic for authorization of my project like login page
registration page reset_password page i have the following code in authorization/urls.py
from django.urls import path
from . import views
urlpatterns =[
path('registration',
views.registration_form),
path('login',
views.login_name),
]
My registration_form view is
from django.shortcuts import render
def registration_form(request):
return
render(request,'registration/login.html',{})
For output my logic on front-end side i created in my project templates directory and inside it registration directory and inside it i put login.html file. in settings.py i have
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'dataflow',
'authorization',
'main',
'django.contrib.postgres',
]
TEMPLATES = [{
'DIRS':
[os.path.join(BASE_DIR,'templates')],
}]
. Than after running server and opened http://127.0.0.1:8000/auth/registration but django erised page not found exception.Can anyone guide me where is error occured???
Are you sure you included this segment in your settings.py ?
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
...
'authorization.apps.AuthorizationConfig', // add this manually
]
The point is that, whenever you create an app, Django will not automatically register it as a part of the framework.
I am following the official Django tutorial on https://docs.djangoproject.com/en/2.2/intro/tutorial01/ but somehow I am not able to run the server as I have created the polls app and added the required urls. When I use the command "py mysite\manage.py runserver" it returns me ModuleNotFoundError: No module named 'polls' error.
Project Folder available at
https://i.stack.imgur.com/bbxfW.png
#views.py
from django.http import HttpResponse
def index(request):
return HttpResponse('<h1><this is a test page</h1>')
#urls.py in polls
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
#urls.py in mysite\mysite
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
#settings.py
INSTALLED_APPS = [
'polls',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
[1]: https://i.stack.imgur.com/bbxfW.png
Well, I resolved it myself. Polls module was not found because it was created outside the project directory. When I removed it and recreated inside the project directory, it was OK now.
also it may be a good idea to cd into yoru django project so you are only running
python manage.py runserver
at first I successfully install django markdown
pip install django-markdown
than I add django-markdown in my setting.py file
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
'django-markdown',
]
then, I change my urls.py like as:
from django.conf.urls import include, url
from django.contrib import admin
from resume import views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^home/$', views.home, name='home'),
url(r'^blog/', include('blog.urls',namespace='blog',app_name='blog')),
url('^markdown/', include('django_markdown.urls')),
]
I also change my admin.py file like as:
from django.contrib import admin
from .models import Post
from django_markdown.admin import MarkdownModelAdmin
class PostAdmin(admin.ModelAdmin):
list_display = ('title','slug','author','publish','status')
list_filter = ('status','created','publish','author')
search_fields = ('title','body')
prepopulated_fields = {'slug':('title',)}
raw_id_fields = ('author',)
date_hierarchy = 'publish'
ordering = ['status','publish']
# Register your models here.
admin.site.register(Post,MarkdownModelAdmin, PostAdmin)
but, when I start my runserver, django gives me an error like this:
ModuleNotFoundError: No module named 'django-markdown'
how can i solve my problem?
In installed apps you should add django_markdown instead of django-markdown