Django urls.py not found - django

Setting up the urls for a Django app called OmniCloud_App. Getting and error when accessing /OmniCloud_App/signup that the url is not found. here is the main urls.py:
urlpatterns = patterns('',
(r'^OmniCloud_App/$', include('OmniCloud_App.urls')),
(r'^admin/', include(admin.site.urls)),
)
which then includes OmniCloud_App/urls.py:
urlpatterns = patterns('OmniCloud_App.views',
(r'^', 'home'),
(r'^signup/', 'signup'),
(r'^(?P<User_id>\d+)/$', 'profile'),
(r'^(?P<User_id>\d+)/social$', 'social'),
(r'^(?P<User_id>\d+)/news$', 'news'),
(r'^(?P<User_id>\d+)/email$', 'email'),
(r'^(?P<User_id>\d+)/photos$', 'photos'),
)
so signup should go to the signup method in views.py:
def signup(request):
return render_to_response('OmniCloud_App/Templates/OmniCloud/signup.html', context_instance=RequestContext(request))
Any reason why this won't work? Here is the 404, which implies that it never got past the initial urls.py file, although visiting simply /OmniCloud_App/ renders the 'home' page correctly (which is also defined in the include('OmniCloud_App.urls')

You need to remove the $ from here
(r'^OmniCloud_App/$', include('OmniCloud_App.urls')),
so that it's:
(r'^OmniCloud_App/', include('OmniCloud_App.urls')),
The $ means end of string.

Related

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

I have a simple return HttpResponseRedirect(reverse('index')) where 'index' is the name of the view. on running the server the index view is correctly displayed but gives out this error "NoReverseMatch at /vehicle_movement/checkinview"on redirecting.
I was working on django 1.1 for the same project but switched to django 2.2 later. Redirect was working fine with url django 1.1 but gives this error with path django 2.2. Another change that i have done is earlier in 1.1 project the index view url was written in the main url.py but now it is written in the apps urls.py.
This is views.py
def index(request):
return render(request,'vehicle_movement/index.html',{})
def CheckinView(request):
if request.method == "POST":
checkin_form = CheckinForm(data = request.POST)
if checkin_form.is_valid():
checkin_form.save()
return HttpResponseRedirect(reverse('index'))
else:
HttpResponse("Error")
else:
checkin_form = CheckinForm()
return render(request,'vehicle_movement/checkin.html',{'checkin_form':checkin_form})
This is the Main urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include(('vehicle_movement.urls','vehicle_movement'),namespace = 'vehicle_movement')),
]
This is app urls.py
app_name = 'vehicle_movement'
urlpatterns = [
path('', views.index, name='index'),
path('index', views.index, name='index'),
]
This is the structure
Warehouse
-Warehouse
-init.py
-settings.py
-urls.py
-static
-templates
-vehicle_movement
-urls.py
-views.py
The TEMPLATES_DIR
TEMPLATES_DIR = os.path.join(BASE_DIR,'templates')
``
You've namespaced your app urls, so you need to use that namespace when reversing them:
reverse('vehicle_movement:index')
But you've also got two paths with the same name in your app urls, which will cause conflicts, if not an error. Delete one of them.
Check name="htmlfilename" in app urls is correct or not.

Make arbitrary url as homepage

I have such a urls.py configuration in project repository "forum"
# Project url
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r"^$", views.index, name="index"),
url(r'^article/', include('article.urls',namespace='article')),
]
and with article/urls.py as
#Article app's url config
urlpatterns = [
# show the article list
url(r"^list/(?P<block_id>\d+)$", views.article_list, name="article_list"),
I attempt to take "^article/list/1$" as home page instead of "views.index".
How to make it redirect to "^article/list/1$" when I issue request "127.0.0.1:8000"?
You can redirect from your views.index. In your views.py
from django.shortcuts import redirect
def index(request):
return redirect('article:article_list')
which should take you to article/list. You can include that block_id parameter in the redirect function.
try this
redirect(reverse('article:article_list', kwargs={'block_id':2}))
and make sure to add kwargs in function like this
article_list(request,**kwargs):
I assume you want to this to debug your article page.
You have two ways of doing this.
Either redirect your index view to article view
views.py
from django.shortcuts import redirect
def index(request):
#We force a redirect to article_list view, and we pass arguments.
#Notice it has to come first.
redirect ("article_list", article_list_argument_view=1)
#it will redirect to localhost:8000/article/list/1
'''
Your Home page code.
'''
Put the url directly before the include
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r"^$", views.index, name="index"),
#The order is important.
url(r'^article/list/1$', "article_list", {'article_list_argument_view':1})
url(r'^article/', include('article.urls',namespace='article')),
]

Django: Issue with url conf

I'm trying to change my project to use class based views, and I'm running into a strange error with my url conf
I have a classed based view:
class GroupOrTeamCreate(AjaxableResponseMixin, CreateView):
model = GroupOrTeam
fields = ['name', 'description']
#success_url = reverse('home_page') # redirect to self
I have the last line commented out because if I don't, django complains that there are no patterns in my url conf.
To start, here's my base urls.py
urlpatterns = patterns('',
url(r'^$', TemplateView.as_view(template_name='core/home.html'), name='home_page'),
url(r'^administration/', include('administration.urls', app_name='administration')),
url(r'^reports/', include('reports.urls', app_name='reports')),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Clearly there are patterns in there. If I comment out the administration urls, it works. So I assume the problem is in there somewhere.
administration urls.py
from django.conf.urls import patterns, url
from .views import ActiveTabTemplateView, GroupOrTeamCreate, GroupOrTeamUpdate
urlpatterns = patterns('',
# Add page
url(r'^add/$', ActiveTabTemplateView.as_view(template_name='administration/add.html'), name='add_page'),
url(r'^add/(?P<active_tab>\w+)/$', ActiveTabTemplateView.as_view(template_name='administration/add.html'),
name='add_page'),
# Seach page
url(r'^search/$', ActiveTabTemplateView.as_view(template_name='administration/search.html'), name='search_page'),
url(r'^search/(?P<active_tab>\w+)/$', ActiveTabTemplateView.as_view(template_name='administration/search.html'),
name='search_page'),
#--------------------------------------------------------------------------
# Forms
#--------------------------------------------------------------------------
# Groups and teams
url(r'^group-or-team-form/$', GroupOrTeamCreate.as_view(template_name='administration/forms/groups_form.html'),
name='group_or_team_form'),
url(r'^group-or-team-form/(?P<pk>\d+)/$',
GroupOrTeamUpdate.as_view(template_name='administration/forms/groups_form.html'),
name='group_or_team_form'),
)
I'm not seeing the problem.. these pages load just fine without that reverse statement in, it appears to be the introduction of the reverse statement that breaks everything but I can't for the life of me work out what the cause is.
You get the error because the URL conf has not been loaded yet when the class is defined.
Use reverse_lazy instead of reverse.
from django.core.urlresolvers import reverse_lazy
success_url = reverse_lazy('home_page')

Django keeps showing loggin form after being logged in

In a Django project I am using the standard authentication system. The user is redirected to /bewerken/dashboard/, which has two links. One of them is linked to /bewerken/inhoud/. However, when clicking the link, the login form is shown again (at url /bewerken/inhoud/). So, the user logs in and is returnd to /bewerken/dashboard again (because that is the 'next' url). And on and on. Don't know what I'm doing wrong!
The urls.py from the project:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^bewerken/', include('zzpwebsites.apps.user.urls')),
)
The urls.py from the app:
urlpatterns = patterns('',
url(r'^inhoud/$', views.bewerken_inhoud, name='bewerken_inhoud'),
url(r'^dashboard/$', views.dashboard, name='dashboard'),
url(r'^', 'django.contrib.auth.views.login', {'template_name': 'user/inloggen.html'}),
)
And the view:
#login_required
def bewerken_inhoud(request):
Usp_1 = Usp.objects.filter(user=request.user)
context = ({'usp_1': usp_1,})
return render(request, 'user/bewerken_inhoud.html' , context)
Thanks!

app name appearing twice in url | Django

On my home page I have a link to a registration page.
In my browser, the home page is at 127.0.0.1:8000/app_name/
When I click the link to register, it takes me to 127.0.0.1:8000/app_name/app_name/register/, which gives a 404 error.
But it should take me to 127.0.0.1:8000/app_name/register/, which displays the proper html.
In my views.py, for the Registration page I have:
return render(request, 'app_name/register.html', {'form': form})
In my app urls.py, for the Registration page I have:
url(r'^register/$', views.register, name='register'),
Why am I getting the duplicate app_name in my url?
Update:
Here is my Project urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^app_name/', include('app_name.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Here is my app_name urls.py:
from django.conf.urls import patterns, url
from app_name import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^register/$', views.register, name='register'),
)
Please add templates dir inside of your project dir and add this path in settings.py(you can add absolute and relate path) add your html pages in this templates folder
read my answer to add path in settings.py
and in your html for link to registration page
Sign Up
your view
return render(request, 'register.html', {'form': form})
Are you declaring the url in your app urls.py or the projects? (I'm wondering how many patterns declarations it's going through)