Changing URL structure getting DoesNotExist error in Django - python

My current urls.py
urlpatterns = patterns('hunt.views',
url(r'^$', views.top100, name='top100'),
url(r'^top100/(?P<slug>.+)/$', views.dj, name='dj'),
url(r'^explore/$', views.explore, name='explore'),
url(r'^explore/(?P<slug>.+)/$', views.explore_dj, name='explore_dj'),
url(r'^monthlytop/$', views.monthlytop10, name='monthlytop10'),
url(r'^trending/$', views.trending, name='trending'),
url(r'^contact/$', views.contact, name='contact'),
url(r'^thanks/$', views.thanks, name='thanks'),
url(r'^faq/$', views.faq, name='faq'),
url(r'^search/(?P<slug>.+)$', views.search, name='search'),
url(r'^sitemap\.xml$', direct_to_template,
{'template': 'sitemap.xml'}),
url(dajaxice_config.dajaxice_url, include('dajaxice.urls')),
)
I want to change my URL from /top100/slug and /explore/slug to just /slug
I did this to my urls.py
urlpatterns = patterns('hunt.views',
url(r'^$', views.top100, name='top100'),
url(r'^(?P<slug>.+)/$', views.dj, name='dj'),
url(r'^explore/$', views.explore, name='explore'),
url(r'^(?P<slug>.+)/$', views.explore_dj, name='explore_dj'),
url(r'^monthlytop/$', views.monthlytop10, name='monthlytop10'),
url(r'^trending/$', views.trending, name='trending'),
url(r'^contact/$', views.contact, name='contact'),
url(r'^thanks/$', views.thanks, name='thanks'),
url(r'^faq/$', views.faq, name='faq'),
url(r'^search/(?P<slug>.+)$', views.search, name='search'),
url(r'^sitemap\.xml$', direct_to_template,
{'template': 'sitemap.xml'}),
url(dajaxice_config.dajaxice_url, include('dajaxice.urls')),
)
I am able to access the landing page and all slugs on that page. But if I try to access explore or monthlytop or any other page I get the DoesNotExist error.

The string "explore" matches the regex for the slug, so it will go to that view. You can put all the single-word views before the slug one, so they'll be matched first.
However you are still left with a problem, in that the regexes for dj and explore_dj are exactly the same. You'll need to have some way of differentiating between them.

Related

Django : Cannot resolve the URLs pattern : Always Index page content is displayed

I am learning Django. I build an app in which I am setting up the URL patterns but in all cases it's displaying the data for the index page.
appTwo urls.py File :
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^$', views.users, name='users'),
url(r'^$', views.help, name='help'),
]
MyProject urls.py file :
urlpatterns = [
url(r'^$', views.index, name='index'),
url('admin/', admin.site.urls),
url(r'^users/', include('appTwo.urls')),
url(r'^help/', include('appTwo.urls')),
]
If I call /users or /help, the browser display the data for index file only.
Is there something with a regex that I am doing wrong?
You're setting your urlpatterns in a wrong way. First of all, in you project's urls.py file you are saying that if a request is sent to /users or /help, then Django should look into your appTwo.urls. When Django gets there, it finds that the urlpatters are set so that everything that's empty after any of the aforementioned urls must be handled by the views.index, views.users and views.help. But as views.index is the first one in the list, then all the request end up being handled by that view.
By the way, if you're using Django >= 2.0 you no longer need to use the url() function, but the path() one instead, for which you can declare the paths as simple strings rather than regular expressions.
You should have something as follows:
MyProject/urls.py
from django.urls import path
urlpatterns = [
path('', include('appTwo.urls')),
path('admin/', admin.site.urls)
]
MyApp/urls.py
from django.urls import path
urlpatterns = [
path('', views.index, name='index'),
path('users/', views.users, name='users'),
path('help/', views.help, name='help'),
]
when django reaches here (in your apps url) while resolving the urls, see all three urls have empty regex as first param (so the first one will be selected), And as you see its index then you always show index page. You need to differentiate the urls. Something like this.
urlpatterns = [
url(r'^users/$', views.users, name='users'),
url(r'^help/$', views.help, name='help'),
url(r'^$', views.index, name='index'),
]
I think you also cant write url index with empty regex at top of other urls, otherwise it will always be selected.

Django tried these URL patterns error 404

my site used to be ok but since I moved my website from my old server to new one suddenly my inner pages does not appear to work (my main page works ok) and I get 404 error with saying none of url patterns match the link.
I think I should change something due to changing the server but I don't know what.
this is my primary urls.py:
urlpatterns = [
url(r'^',include('BestOfBrands.urls')),
url(r'^admin/', admin.site.urls),
# url(r'^accounts/', include('django.contrib.auth.urls')),
url(r'^accounts/', include('allauth.urls')),
url(r'^search/', include('haystack.urls')),
]
and this is my urls.py file which is in "BestOfBrands" Folder (my webapp folder):
urlpatterns= [
url(r'^$', views.index, name='index'),
url(r'^(?P<cg>[-\w]+)-Category/$', views.category, name='category'),
url(r'^(?P<cg>[-\w]+)-Category/(?P<scg>[-\w]+)-SubCategory/(?P<s2cg>[-\w]+)-Brands/$', views.sub2category, name='sub2category'),
url(r'^(?P<cg>[-\w]+)-Category/(?P<scg>[-\w]+)-SubCategory/(?P<s2cg>[-\w]+)-Brands/(?P<br>[-\w]+)/$', views.brand, name='brand'),
url(r'^(?P<sub2category_id>[0-9]+)/vote/$', views.vote, name='vote'),
url(r'^(?P<brand_id>[0-9]+)/comment/$', views.comment, name='comment'),
url(r'^(?P<comment_id>[0-9]+)/replycomment/$', views.replycomment, name='replycomment'),
url(r'^(?P<s2url>[-\w]+)/addbrand/$', views.addbrand, name='addbrand'),
url(r'^(?P<usern>[-\w]+)-Profile/$', views.profile, name='profile'),
url(r'^Change-Avatar/$', views.changeavatar, name='changeavatar'),
url(r'^upload-avatar/$', views.uploadav, name='uploadav'),
url(r'^(?P<comment_id>[0-9]+)/like/$', views.like, name='like'),
url(r'^(?P<rcomment_id>[0-9]+)/likereply/$', views.likereply, name='likereply'),
]

Django 2.0: Url path to homepage/index

I'm trying to set my django homepage, linked to the root of my website: i.e. http://127.0.0.1:8000/index/ but instead, I keep getting errors where Django is searching for myapp as the homepage: http://127.0.0.1:8000/myapp/. I would just like to land on a homepage with "index" in the url instead of "myapp/"
The error is as follows:
Using the URLconf defined in Main.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
[name='myapp_index']
publications/ [name='publications']
^static\/(?P<path>.*)$
The current path, myapp/, didn't match any of these.
Views.py
def index(request):
return render(request, 'index.html')
Main/urls
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
myapp/urls
from django.urls import path
from logbook import views
urlpatterns = [
path('', views.index, name='index'),
path('', views.myapp_index, name='myapp_index'),
path('publications/', views.publications, name='publications'),
]
Now, if I change the path in Main/urls.py to path('myapp/', include('myapp.urls')), I land on the appropriate homepage, except, I would prefer that there is "index/" listed in the URL instead of "myapp/".
I may be missing something very simple here. This is what I think is my MWE, happy to add more.
I was having the same problem and I was cracking my head finding a solution.
The solution was to erase browser's cache and history
I realized that because when I tried with incognito mode worked perfectly!
This should be in the main urls.py
path('', views.index, name='index')
instead of my_app/urls.py
path('myapp/', include('myapp.urls'))
path('index/', include('myapp.urls'))
That might get the desired result.
Did you try following:
Main/urls
...
path(r'^', include('myapp.urls')),
...
myapp/urls
...
path(r'^$', views.index, name='index'),
# path('', views.myapp_index, name='myapp_index'), ## REMOVE THIS ROW ##
...
first you must choose one of below lines in myapp.urls and delete other
you can not set two views for one url patterns
path('', views.index, name='index'),
path('', views.myapp_index, name='myapp_index'),
you have two solution for get index/ as main url
1.change Main.urls like below
path('', include('myapp.urls')),
to
path('index/', include('myapp.urls')),
2.change myapp.urls like below
path('', views.index, name='index'),
to
path('index/', views.index, name='index'),
you actually changed name of urls that using in python codes not path of urls
edit
Do you mean index/ like the following?
www.example.com/
so u must do that as above but delete "index/"
so u cant do it as below :
change Main.urls like below
path('', include('myapp.urls')),
to
path('^$', include('myapp.urls')),
or
change myapp.urls like below
path('', views.index, name='index'),
to
path('^$', views.index, name='index'),
This is what worked for me (Django2) - pointing to index action in 'static_pages' app's views.py as home page:
main urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('static_pages.urls')), #included urls from static_pages app
]
static_pages/urls.py:
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name="index"),
]
static_pages/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("First Django2 Message!")
Note that 'static_pages' app is added to INSTALLED_APPS in settings.py.
Now, calling http://127.0.0.1:8000/ I got: "First Django2 Message!".

Reverse for 'password_reset_done' not found. 'password_reset_done' is not a valid view function or pattern name

I have a problem with django's view "password_reset_done".
When I try to open accounts/reset-password I have this error.
But if I open url accounts/reset-password/done it works.
URLS.PY of "accounts" app
from django.conf.urls import url
from django.contrib.auth.views import login , logout, password_reset,
password_reset_done
from . import views
urlpatterns = [
url(r'^register/$', views.register, name='register'),
url(r'^profile/$', views.profile, name='profile'),
url(r'^profile/prpage/(\d+)/$', views.profile, name='prpage'),
url(r'^profile-edit/$', views.profiledit, name='profile-edit'),
url(r'^login/$', login ,{'template_name':'accounts/login.html'},
name='login'),
url(r'^logout/$', views.logout_view, name='logout'),
url(r'^profile/(?P<proj_id>\d+)/$', views.userprojectpage,
name='userprojectpage'),
url(r'^changepassword/$', views.changepassword, name='changepassword'),
url(r'^reset-password/$', password_reset, name='reset_password'),
url(r'^reset-password/done/$', password_reset_done,
name='password_reset_done'),
]
please help! Thanks in advance)
Add success_url parameter of Class Based View PasswordResetView. This will replace default route of password_reset_done
from django.urls import path, re_path, include, reverse_lazy
path('reset/',PasswordResetView.as_view(
template_name='password_reset.html',
email_template_name='password_reset_email.html',
subject_template_name='password_reset_subject.txt',
...
success_url = reverse_lazy('accounts:password_reset_done')
...
...
),name='password_reset'),
11 there are some change made look like password_reset, password_reset_done not able to import therefore display this error regarding this url https://github.com/django/django/blob/stable/1.11.x/django/contrib/auth/views.py.
There are two way to resolve the error above.
to resolve this error add url(r'', include('django.contrib.auth.urls')), to main urls.py in your project. This will stopped the error and keep your urls in your accounts:
url(r'^reset-password/$', password_reset, name='reset_password'),
url(r'^reset-password/done/$', password_reset_done,
name='password_reset_done'),
I have resolved this error like above and it works fine.
Second way to resolve the error is different than first option above,remove the url(r'', include('django.contrib.auth.urls')), from main urls.py file and in your accounts/urls.py add following url's:
url(r'^reset-password/$', password_reset,
{'template_name':'reset_password.html',
'post_reset_redirect':'accounts:password_reset_done',
'email_template_name': 'reset_password_email.html'},
name='reset_password'),
url(r'^reset-password/done/$', password_reset_done,
{'template_name': 'reset_password_done.html'}, name='password_reset_done'),
url(r'^reset-password/confirm/(?P[0-9A-Za-z]+)-(?P.+)/$',
password_reset_confirm, {'template_name': 'reset_password_confirm.html',
'post_reset_redirect':'accounts:password_reset_complete'},
name='password_reset_confirm'),
This is the second solution and working.
I hope that helps your problem and sorry for delay respond your last comment, I was away.
DilMac

how can send blog.urls to my main page

I have a blog application and application has a urls.py which includes generic view
urlpatterns = patterns("",
url(r'^', ListView.as_view(
queryset=Post.objects.all().order_by("-created")[:2],
template_name='index.html')),
)
I send 2 data.
In main project urls.py like that:
urlpatterns = patterns("",
url(r"^$", direct_to_template, {"template": "index.html"}, name="home"),
url(r"^admin/", include(admin.site.urls)),
url(r"^blog/", include('blog.urls')),
)
I can see retrieved datas from db 127.0.0.1/blog/ I want to also see in 127.0.0.1 I mean in the start page.
I add this:
url(r"^$", include('blog.urls), direct_to_template, {"template": "index.html"}, name="home"),
But does not work. How can I achieve this?
This should Work, seems you have missed the closing of single quotes on your urls.py.I am using this and it works fine.
(r'^$',process.internal.views.HomePage.as_view(template_name='homepage.html')),

Categories

Resources