Django website loads with empty root urls.py file - django

I know it's feels like elementary, and yet I can't come up with a clean solution based on doc only.
I have the following project structure (I omit files like models.py, forms.py for the purpose of keeping the question concise)
hello_world
hellow_world
urls.py
app_2
urls.py
app_3
urls.py
manage.py
urls.py
settings.py
As you see, my goal is to have a separate urls.py file for each app, and then assemble them into root urls.py (depicted at the same level as settings.py in the list above). The problem is that my root urls.py is EMPTY (!!!) now, and the site still loads the home page !!! What am I doing wrong ???
See the details below:
settings.py:
ROOT_URLCONF = 'urls'
hellow_world urls.py:
urlpatterns = [
url(r'^$', views.home , name = 'home'),
]
root urls.py - empty !
manage.py:
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)

Use include() to include more urls:
# your main urls.py file
from django.conf.urls import include, url
urlpatterns = [
url(r'^myapp1/', include('myapp1.urls')),
url(r'^myapp2/', include('myapp2.urls')),
]
And:
# myapp1/urls.py
from django.conf.urls import url
from . import views
app_name = 'myapp1'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
#...
]

Related

I ruined the admin page, how do I fix it? ( Django 4.1 )

I have followed the steps from the django tutorial ( https://docs.djangoproject.com/en/4.1/intro/tutorial01/ ) and once I'd finished all the steps I stopped developing the project for 2 weeks at least without giving it much thought.
Once I've decided to check how the pages were looking like that's when I found this:
The admin page
And I don't know how I did it.
Here's how the documentation looks like:
meuSite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
polls/
migrations/
static/
templates/
admin/
base.html
polls/
detail.html
index.html
results.html
__init__.py
admin.py
apps.py
models.py
tests.py
urls.py
views.py
db.sqlite3
manage.py
SETTINGS:
meuSite/settings.py
INSTALLED_APPS = [
'polls.apps.PollsConfig',
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
]
URLS:
# meuSite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path("admin/", admin.site.urls),
path("polls/", include('polls.urls')),
]
# polls/urls.py
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:question_id>/', views.DetailView.as_view(), name='detail'),
path('<int:question_id>/results/', views.ResultsView.as_view(), name='results'),
path('<int:question_id>/vote/', views.vote, name='vote')
]
If there's something else that I didn't show that may have the root of the problem, please tell me
I tried solving it by deleting the templates/admin directory, but it didn't change how the page looks

django favicon icon does not display

I have the following django project file structure:
lecture3/
lecture3/
urls.py
tasks/
static/
img/
favicon.ico
urls.py
my tasks/urls.py file is:
from django.urls import path
from . import views
from django.contrib.staticfiles.storage import staticfiles_storage
from django.views.generic.base import RedirectView
app_name = 'tasks'
urlpatterns = [
path("",views.index, name="index" ),
path("add",views.add, name="add" ),
path('favicon.ico', RedirectView.as_view(url=staticfiles_storage.url('img/favicon.ico'))),
]
but when I run I get a 404 error. How do I fix this?
Try looking in your settings.py file for STATIC_URL = '/static/'. I moved my favicon.ico into this directory and my problem went away. I don't have it in my urlpatterns.

django.urls.exceptions.NoReverseMatch: 'admin' is not a registered namespace

I have a Django projects with two apps, "projects" and "codebox", they were running fine, then at some point I got the following error:
django.urls.exceptions.NoReverseMatch: 'admin' is not a registered
namespace
If I remove the link to my admin panel from my template this error goes away, but then I can't get to my admin panel:
Admin
I was working in the urls.py files when this error occurred, have I changed something that is inadvertently having an impact on the admin link?
Here is my top level urls.py file:
from django.contrib import admin
from django.urls import include, path, re_path
urlpatterns = [
# path('', include('projects.urls')),
path('projects/', include('projects.urls')),
re_path('^accounts/', include('django.contrib.auth.urls')),
re_path('^logged_out/', include('projects.urls')),
path('codebox/', include('codebox.urls')),
]
Here is my urls.py for "projects":
from django.urls import path, include
from . import views
app_name = 'projects'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('insight/', views.InsightView.as_view(), name='insight'),
path('logged_out/', views.LoggedoutView.as_view(), name='loggedout'),
path('insight/subgen/', views.SubgenView.as_view(), name='subgen'),
]
And here is urls.py for my second app, codebox:
from django.urls import path, include
from . import views
app_name = 'codebox'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('form/', views.CreateView, name="form"),
]
add this in top of project/urls.py
from django.conf.urls import include, url
then add this in the url_pattern
url(r' ', include(('<app_name>.urls','<app_name>'), namespace='<app_name>')),
hope this will work
Check your base html file which you're extending to other html pages. If there is some space between urls tag remove that space and save your file and try again you can check below code for ref.
from django.contrib import admin
from django.urls import path, include
from passapp import views
urlpatterns = [
path('', views.index, name='index'),
path('admin/', admin.site.urls),
path('passapp/', include('passapp.urls')),
]
href="{% url 'admin:index' %}"

django url is giving page not found (404) error

I am getting page not found(404) error for votings app that I created from datacamp tutorial. I have checked my code to make sure it's free of errors. admin is working fine but other urls are not.
Here's urls.py code from the main application directory:
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('blog/', include('blog.urls')),
path('votings/',include('votings.urls')),
path('admin/', admin.site.urls),
]
Here's urls.py from the votings app directory:
from django.urls import path
from . import views
urlpatterns = [
path('',views.index, name='index'),
path('<int:question_id>/',views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
I am using django 2.0.5.
Thanks
Unless you've made a mistake copying the wrong urls.py for votings app, the problem has to be it.
This is the main urls.py of your project:
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('blog/', include('blog.urls')),
path('votings/',include('votings.urls')),
path('admin/', admin.site.urls),
]
FYI, according to the docs include() adds urls from your app directory's (in your case it's voting) urls.py to the main urls.py (in memory). This keeps the main urls.py from getting too big to read.
And this is the urls.py of your votings app which is literally the copy of main urls.py:
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('blog/', include('blog.urls')),
path('votings/',include('votings.urls')),
path('admin/', admin.site.urls),
]
Don't you see any problem here? There's no endpoint. Where's the associated view (function-based or class based) for this url?
I suggest writing a view in your views.py and test it out:
Votings app views.py:
from django.http import HttpResponse
import datetime
def current_datetime(request):
now = datetime.datetime.now()
html = "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)
Votings app urls.py:
from django.urls import include, path
from . import views
urlpatterns = [
path('home/', views.current_datetime, name='home'),
]

Python-Django : ImportError at / : No module named urls

I have created Djnago project in eclipse. Unfortunately, i am facing issue when i run the project
ImportError at /
No module named urls
Here Error Page
http://dpaste.com/1499981/
Eclipse Project http://i1008.photobucket.com/albums/af204/shoaibshah01/Untitled_zps84f95b4f.jpg
urls.py Content
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'TestApp.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Try converting admin.site.urls to string url(r'^admin/', include('admin.site.urls'))
Most likely this is because your TestApp is not referenced in INSTALLED_APPS so Django have no idea it should process it.
Try to change your ROOT_URLCONF to 'urls' value in settings.py
ROOT_URLCONF = 'urls'
The link you provided for the error log is giving 404.
Perhaps you can try with the below code for admin in URLs.py
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
Everything is looking correct in settings.py.
If the above changes are not working, please share error page again.