I want a reverse url into a variable. I have to apps. one account and another profiles.
my main url
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.index, name='index'),
url(r'^account/', include('account.urls', namespace="account")),
url(r'^profiles/', include('profiles.urls', namespace="profiles")),
)
my profiles app url
urlpatterns = patterns('',
url(r'^view/(?P<username>\w+)/$', views.profile, name="profile"),
)
my account app url
urlpatterns = patterns('',
url(r'^message/add_new/$', views.new_message, name='new_message'),
)
my account app view
def new_message(request):
if request.user.is_authenticated():
sender_username = request.user.username
sender_url = reverse('prfiles.views.profile', args=(sender_username,))
but I got 'NoReverseMatch at' error. Please suggest me a better way to solve this.
IMHO, prfiles.views.profile seems like typo.
Fix typo (if it is) or use the URL pattern name instead of the view name.
sender_url = reverse('profiles:profile', args=(sender_username,))
See Reversing namespaced URLs | Django documentation.
Related
I am trying to attach hobbieswithCSS.html file to my website, while using Django. I am a beginner when it comes to Django, so I have naturally came across some problems (in the title) like this.
I have this anchor tag on my homepage -
My Hobbies
I have this view in my views.py file -
def hobbieswithCSS(request):
return render(request,'basic_app/hobbieswithCSS.html')
I think, that the main problem will be with the urlpatterns in urls.py file, because I am not sure how to set it up. These are my urlpatterns -
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^admin/', admin.site.urls),
url(r'^basic_app/',include('basic_app.urls')),
url(r'^logout/$',views.user_logout,name='logout'),
url(r'^$', views.hobbieswithCSS, name='hobbieswithCSS'),
]
Could anybody please tell me, how could I change the code in order to make that hobbieswithCSS.html file display, when I am trying to run it on my server?
It must be:
My Hobbies
Without basic_app.
Also you have the same path for index and hobbies . You have to change your url path:
urlpatterns = [
url(r'^$', views.index, name='index'),
...
url(r'^hobbies/$', views.hobbieswithCSS, name='hobbieswithCSS'),
]
in your template update to
My Hobbies
I believe this is a simple question but I am having a hard time figuring out why this is not working.
I have a django project and I've added a second app (sales). Prior to the second app, my urls.py simply routed everything to the first app (chart) with the following:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('chart.urls')),
]
and it worked fine.
I have read the docs over and over a looked at many tutorials, so my impression is that I can simply amend the urls.py to include:
urlpatterns = [
path('admin/', admin.site.urls),
path('sales/', include('sales.urls')),
path('', include('chart.urls')),
]
and it should first look for a url with sales/ and if it finds that then it should route it to sales.urls, and if it doesn't find that then move on and route it to chart.urls. However, when I load this and type in 127.0.0.1:8000/sales, it returns the following error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/sales/
Raised by: chart.views.index
which tells me that it is not routing my sales/ url to sales.urls but to chart.urls. When I change path('', include('chart.urls')), to path('', include('sales.urls')), it does route it to sales.urls so I know that my sales.urls file is set up correctly.
I know this is probably an easy question but I cannot figure it out with anything I've read. Any help is appreciated.
chart.urls:
from django.urls import path, re_path
from . import views
urlpatterns = [
path('dashboard/', views.chart, name='dashboard'),
path('', views.index, name='index', kwargs={'pagename': ''}),
path('<str:pagename>/', views.index, name='index'),
]
sales.urls:
from django.urls import path
from . import views
urlpatterns = [
path('sales/', views.sales, name='Sales Dashboard'),
]
I think the correct url for the sales page in your case would be http://127.0.0.1:8000/sales/sales/.
This is because sales/ is then looking in the included sales.urls and not finding any urls at the root / (the only url included there is at sales/ (within sales/) so will be sales/sales/
Reply to an OLD question but may be useful for others...
from django.urls import path, include
dont for get to import include
Do this:
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('chart.urls')),
url(r'^sales/', include('sales.urls')),
]
you are not doing it properly since you are not telling your app where go once it is loaded .this r'^' to go directly to chart.url url(r'^', include('chart.urls')),
I checked other pages to figure it out and I looked URL dispatcher but couldn't find a solution to this.
When I click to change the page from navbar nothing happens. homepage is extends to the header.html but others are not. infact when I click to another button change the page it stays still. here are my codes;
home/first/views.py
from django.shortcuts import render
def index(request):
return render(request, 'first/home.html')
def contact(request):
return render(request, 'first/iletisim.html', {'content':['Eger bizimle iletisime gecmek isterseniz mail adresimizi kullanabilirsiniz.', 'gulumhali#outlook.com']})
def home (request):
return render(request, 'first/home.html')
home/first/urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^', views.index, name = 'index'),
url(r'^home/', views.index, name = 'home'),
url(r'^contact/', views.contact, name = 'iletisim'),
]
main urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('first.urls')),
url(r'^blog/', include('blog.urls')),
]
SOLUTION:
Like Adam said below; if you having this problem change the order home/first/urls.py for views.index.
Your first URL is catching everything. Essentially it's saying if the URL has a start to it, send to index.
Since it's the first url in the list, it's always going to catch that first since it goes through the url patterns and finds the first one it matches. As you've found out in the comment, if you move it to the bottom, when you go to either home/ or contact/ it will catch those first.
However, you still will have a problem with that because if you go to asdfblahblah/ or anything, it's still going to catch that. What you should do is add a $ to show that there should be nothing in the URL to route to index. That regex says if there is a start, and an end, and nothing in between, route to index.
urlpatterns = [
url(r'^$', views.index, name = 'index'),
url(r'^home/', views.index, name = 'home'),
url(r'^contact/', views.contact, name = 'iletisim'),
]
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")),
I'trying to redirect the / of my domain to point to a index in my "frontend" app.
I tried a lot of ways and all of them work.
The problem is that my index_view is being called twice for every redirect.
Here is my top urls.py
urlpatterns = patterns('',
url(r'^$', lambda x: HttpResponseRedirect('/frontend/')),
url(r'^frontend/', include('frontend.urls', namespace="frontend")),
)
And here is my frontend/urls.py
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^alert/create/$', views.create_alert, name="create_alert"),
url(r'^alert/edit/(\w+)', views.edit_alert, name="edit_alert"),
)
Every time I go to / is calling my views.index twice and I can't see why =/
Am I doing the redirecting wrong ?
Thanks in advance for any help!
You can set the root to use your FE url patterns like this:
urlpatterns = patterns('',
url(r'^', include('frontend.urls', namespace="frontend")),
)
If you wanna forcibly redirect to /frontend/ then you will need a view to handle the redirect.
Maybe look at the Redirect Generic view: https://docs.djangoproject.com/en/1.1/ref/generic-views/#django-views-generic-simple-redirect-to