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'),
]
Related
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.
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!".
I have in my main urls:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('scoring.urls', namespace='scoring')),
]
and in my app urls:
urlpatterns = [
url(r'scoring/(?P<query_id>[0-9]+)/$', views.get_table_data, name='table_api'),
url(r'^scoring/$', views.index, name='index'),
]
and in my template:
<li>Scoring</li>
but what {% url 'scoring:index' %}generates is localhost/instead of localhost/scoring. Why?
At first, you can add the ^scoring prefix in the main urls.py file, instead of writing it everywhere in your scoring urls:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^scoring/', include('scoring.urls', namespace='scoring')),
]
Then in your scoring urls.py make sure to add app_name:
app_name = 'scoring'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<query_id>[0-9]+)/$', views.get_table_data, name='table_api'),
]
(Note that I removed the scoring prefix in the url patterns.)
Now, as you've added app_name, the reversing in your template should work as expected.
I have a blog made in django in a VPS. The blog is working fine but to access it I have to write the url example.com/blog/
What I'm trying is to make an automatic redirection so when a user enters example.com/ it automatically redirects to example.com/blog/
The project is set under apache.
This is my the configuration in myproject/urls.py:
urlpatterns = [
url(r'^blog/', include('blog.urls', namespace="blog")),
url(r'^admin/', include(admin.site.urls)),
]
This is the configuration of myproject/blog/urls.py that right now is formed by a post list and a post detail:
urlpatterns = patterns('',
# Index
url(r'^(?P<page>\d+)?/?$', ListView.as_view(
model=Post,
paginate_by=5,
),
name='index'
),
# Individual posts
url(r'^(?P<pub_date__year>\d{4})/(?P<slug>[a-zA-Z0-9-]+)/?$', DetailView.as_view(
model=Post,
),
name='post'
),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I already tried to add a .htaccess with different configurations but it's not working.
Is there a way to redirect from django?
If you're sure that you want that / to redirect to /blog/ for that you can create a temporary index view and redirect the response to /blog/
url(r'^', temp_index, name='index'),
def temp_index(request):
return HttpResponseRedirect('/blog/')
If you want that / must show /blog then replace
url(r'^blog/', include('blog.urls', namespace="blog")), with
url(r'^', include('blog.urls', namespace="blog")),
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.