django - invalid syntax (urls.py, line 7) - django

I'm doing a slight variation on my urls.py from the tutorial where I have the following -
mysite/urls.py -
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^TidalDEV/', include('TidalDEV.urls')),
)
TidalDEV/urls.py -
from django.conf.urls import patterns, url
from TidalDEV import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index')
url(r'^(?P<pk>[0-9]+)/$', views.tesxml, name='tesxml'),
)
And this is the view in views.py -
def tesxml(self, request, pk, format=None, renderer_context=None):
"""
returns an XML of a jobmst listing
"""
template_vars['jobmst'] = (queryset1, [pk])
template_vars['jobdtl'] = (queryset2, [pk])
template_vars['jobdep'] = (queryset3, [pk])
t = loader.get_template('TidalAPI/templates/xml_template.xml')
c = Context(template_vars)
return HttpResponse(t.render(c), mimetype="text/xml")
When I try to hit my url at http://localhost:8080/TidalDEV/10081/ I get invalid syntax. What is the problem here?
Essentially I need the view to populate a template XML file I built.

You are missing a comma after your index view in TidalDEV/urls.py

Related

ERROR IN ADDING ANOTHER TEMPLATE IN EXISTING APP

Using the URLconf defined in rldjango.urls, Django tried these URL patterns, in this order:
[name='index']
<int:destinations_id [name='destionations']
admin/
accounts/
^media/(?P<path>.*)$
The current path, destinations.html, didn't match any of these.
***"***from main urls.py******
urlpatterns = [
path('', include('travello.urls')),
path('admin/', admin.site.urls),
path('accounts/',include('accounts.urls')),
]
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
*from urls.py travello *
from django.urls import path
from . import views
urlpatterns = [
path('',views.index,name='index'),
path('<int:destinations_id', views.destinations, name='destionations')
views.py of travello
from django.shortcuts import render
from .models import Destination
# Create your views here.
def index(request):
dests=Destination.objects.all()
return render(request,"destinations/index.html",{'dests':dests})
def destinations(request):
return render(request, "index.html")
please tell what i am doing wrong
i am a newbii
i am wanting to add one more template to my app index.html is added and i am trying to add destinations.html to it
i am taking tutorials from telesko django course
please help

Using the URLconf defined in tango_with_django_project.urls, Django tried these URL patterns, in this order:

I've been going through the Tango With Django 1.7 guide and I've come up with the following issue that I'm having troubles fixing:
Using the URLconf defined in tango_with_django_project.urls, Django
tried these URL patterns, in this order:
1. ^$ [name='index']
2. ^about/$ [name='about']
3. ^category/(?P<category_name_slug>[\w\-]+)/$ [name='category']
4. ^admin/
5. ^rango/$
6. ^about/
7. ^category/
8. ^media/(?P<path>.*)
The current URL, rango/category/python, didn't match any of these.
This is my rango/urls.py file
from django.conf.urls import patterns, url
from rango import views
urlpatterns = patterns(' ',
url(r'^$', views.index, name='index'),
url(r'^about/$', views.about, name='about'),
url(r'^category/(?P<category_name_slug>[\w\-]+)/$',
views.category, name='category')
)
In tango_with_django_project/urls.py I have
from django.conf.urls import patterns, include, url
from django.contrib import admin
from rango import views
from django.conf import settings
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^about/$', views.about, name='about'),
url(r'^admin/', include(admin.site.urls)),
url(r'^rango/$', include('rango.urls')),
url(r'^about/', include('rango.urls')),
)
if settings.DEBUG:
urlpatterns+= patterns(
'django.views.static',
(r'^media/(?P<path>.*)',
'serve',
{'document_root': settings.MEDIA_ROOT}),
)
In rango/views.py I have:
from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import render
from rango.models import Category
from rango.models import Page
def index(request):
category_list = Category.objects.order_by('-likes')[:5]
context_dict = {'categories': category_list}
return render(request, 'rango/index.html', context_dict)
def about(request):
return HttpResponse("Rango says: Hello world! <br/> <a
href='/rango/about'>About</a>")
def category(request, category_name_slug):
context_dict = {}
try:
category = Category.objects.get(slug=category_name_slug)
context_dict['category_name'] = category.name
pages = Page.objects.filter(category=category)
context_dict['pages'] = pages
context_dict['category'] = category
except Category.DoesNotExist:
return render(request, 'rango/category.html', context_dict)
I've been following the guide so I'm not quite sure what the problem is; I had a similar problem earlier on in the guide when I was getting the same error but with /about (never got this fixed).
Any idea what might be causing this problem? Any help is appreciated!
As already commented, the problem here is the definition of the URLconf, more precisely the inclusion of another URLconf that shall define every url that starts with rango/.... The crucial line is this one:
url(r'^rango/$', include('rango.urls')),
which should read instead
url(r'^rango/', include('rango.urls')),
The regex '^rango/$' captures: <start-of-string>rango/<end-of-string>
The regex '^rango/' captures: <start-of-string>rango/<whatever follows>, and that's where your included urls kick in.

Django Page not found (404) error with url config

I have a problem with urls config in my Django project, in my root url config i have this:
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', include('main.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^profile/', include('accounts.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()
And in my app url config i have this:
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'^$', 'main.view.home', name='main_home'),
url(r'^integrations/$', 'main.view.integrations', name='main_integrations'),
)
But when I go to render this url http://127.0.0.1:8000/integrations/ I obtain an error 404 Not Found.
Thanks by yours answers.
Yes, my app named 'main' and this is my view code:
def home(request):
user = request.user
if not user.is_authenticated():
data = {
'messages': get_messages(request),
}
return render_to_response('home.html', data, context_instance=RequestContext(request))
data = {
'messages': get_messages(request),
}
return render_to_response('home_user.html', data, context_instance=RequestContext(request))
def integrations(request):
return render_to_response('/main/integrations.html', context_instance=RequestContext(request))

what's wrong with my urls.py in this example?

I just installed userena, and had the example working from the tutorial, but as soon as I added in a single line in URLS.py, I'm getting an error. In the example below, I added the line mapping the home function from views.py
Now the issue I'm having is that when I go to 127.0.0.1/8000, I get TypeError: string is not callable, but then oddly, if I go to accounts/signup or accounts/signin, I am getting the template that should be appearing if i go to 127.0.0.1/8000.
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.views.generic import TemplateView
from accounts import views
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r"^$", 'home'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
Here is my accounts/views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
def home(request):
return render('homepage.html')
You need to remove the quotes in the url and import that view
from accounts.views import home
urlpatterns = patterns('',
url(r"^$", home),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
You can steel use the strings in the url() but you must use the format 'app.views.viewname'
urlpatterns = patterns('',
url(r"^$", 'accounts.views.home'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
Or name the module in the first argument as string to patterns()
urlpatterns = patterns('accounts.views',
url(r"^$", 'home'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
the issue was I forgot to include the request in the return render.
The correct answer is that render is being called incorrectly. Actually, the views.py file would raise a SyntaxError, but we'll let that slide :)
# views.py
from django.shortcuts import render
def home(request):
return render(request, 'homepage.html')

Django. Confusion about redirections

Here is my simplified code:
project's urls:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^to_app1/', include('app1.urls')),
url(r'^to_app2/', include('app2.urls')),
)
app1's urls:
from django.conf.urls import *
from app1 import views
urlpatterns = patterns('',
url(r'^$', views.app1Page, name='app1-page'),
url(r'^action/', views.app1_to_app2, name='app1-to-app2-page'),
)
app2's urls:
from django.conf.urls import *
from app2 import views
urlpatterns = patterns('',
url(r'^$', views.app2Page, name='app2-page'),
)
app1's view:
def app1Page(request):
return render(request, 'app1/app1page.html')
def app1_to_app2(request):
# some actions going on here #
return HttpResponseRedirect('/to_app2/')
app2's view:
def app2Page(request):
return render(request, 'app2/app2page.html')
So, I have a button on app1page.html which should sends data through POST function to to_app1/action/ url. In the end it should redirect me to app2page.html, but it does not. It gives me GET 127.0.0.1:8000/to_app2/ HTTP/1.0 200 OK, but I staying on the same page (app1page.html).
There is obviously something I missing, could someone point out it to me?