urls.py
from django.conf.urls import include, url
from . import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
from django.views.generic import TemplateView
from django.conf import settings
from .views import Search
app_name = 'base'
urlpatterns = [
url(r'^index/$', Search.as_view(), name='index'),
url(r'^newresume/$', views.newresume, name='newresume'),
url(r'^profile/(?P<pk>[0-9]+)/$', views.profile, name='profile'),
url(r'^update_info/(?P<pk>[0-9]+)/$', views.update_info, name='update_info'),
]
views.py
def profile(request, pk):
student = get_object_or_404(Students, pk=pk)
return render(request, 'base/profile.html', {'student': student})
_tweet_search.html
<div class="studentfio">
<h4>{{ student.job }}</h4>
</div>
Console error
Hi all, help me with this error, please. I made a AJAX-request, after this i get an error, how can i fix that ?
<div class="studentfio">
<h4>{{ student.job }}</h4>
</div>
as you have mentioned app_name in your urls.py file, you need to mention that in your url
Related
<a class="nav-link active" aria-current="page" href="{% url 'tasks' %}">Tasks</a>
When I'm using this template tag I get an error no ReverseMatch.
but ,
<a class="nav-link active" aria-current="page" href="/tasks">Tasks</a>
When I'm doing this I get no error.
Can someone explain me?
urls.py of the project
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('tracker.urls')),
path('tasks/', include('tasks.urls')),
]
urls.py of the tasks app
from django.urls import path
from . import views
urlpatterns = [
path('', views.tasklist, name="tasklist"),
]
views.py of the tasks app
from django.shortcuts import render
def tasklist(request):
return render(request, 'tasks/task_list.html')
I am getting the below error when I am trying to load the home page:
Reverse for 'display_data' not found. 'display_data' is not a valid view function or pattern name
My views.py file is as follows:
def home(request):
#query_results = QRC_DB.objects.all()
return render(request, 'display_data.html')
def display_data(request,component):
#query_results = QRC_DB.objects.all()
return HttpResponse("You're looking at the component %s." % component)
My urls.py file under the app is as follows:
from django.urls import path
from fusioncharts import views
urlpatterns = [
path('home/', views.home, name=''),
]
The urls.py file under the project is as follows:
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('fusioncharts.urls'))
]
And my html file (display_data) code is as follows :
{% block content %}
<h3>Display the test results</h3>
<div id="container" style="width: 75%;">
<canvas id="display-data"></canvas>
<li>SQL</li>
</div>
{% endblock %}
Can anyone please help me to find out the mistake ?
Thanks.
Your urls.py file doesn't contain any url for display_data.
When you're trying to click the link rendered in the HTML tag, namely,
<li>SQL</li>
it tries to resolve the URL display_data.
First, it checks the root urls.py file. Among the following:
path('admin/', admin.site.urls),
path('', include('fusioncharts.urls'))
it matches the second one. Then it loads the fusioncharts.urls but the fusioncharts.urls doesn't contain any URL for display_data. That's why you are getting the error.
The urls.py file should be like this:
from django.urls import path
from fusioncharts import views
urlpatterns = [
path('home/', views.home, name=''),
path('display_data/<str:arg>', views.display_data, name='display_data'),
]
# There is a change in urls.py and in your template 'display_data.html'
urls.py
urlpatterns = [
path('home/', views.home, name=''),
path('display_data/<str:component>', views.display_data, name='display_data'),
]
display_data.html
{% block content %}
<h3>Display the test results</h3>
<div id="container" style="width: 75%;">
<canvas id="display-data"></canvas>
<li>SQL</li>
</div>
{% endblock %}
I'm trying to render index HTML and get post title from database but I'm getting error. I define in views post database but still getting error
name 'Post_title' is not defined
my app/views.py
from django.shortcuts import render, get_object_or_404
from django.shortcuts import reverse
from .models import BlogPost,comments
def index(request):
Post_list = BlogPost.objects.all()
template_name = 'front/index.html'
return render(request, template_name,{Post_title:"Post_title",})
def post_detail(request):
return render(request, 'front/post_detail.html')
my app/urls.py
from django.urls import path
from .import views
urlpatterns = [
path('', views.index, name = 'index'),
path('<int:BlogPost_id>/', views.post_detail, name='Post Detail')
]
my project/urls.py
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
from froala_editor import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
path('froala_editor/', include('froala_editor.urls'))
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
my index.html template
<div class="col-md-8 mt-3 left">
{% for post in Post_list %}
<div class="card mb-4">
<div class="card-body">
<h2 class="card-title">{{ post.Post_title }}</h2>
</div>
</div>
{% endfor %}
</div>
You are not sending the Post_list to the template through context. Send it like this
return render(request, template_name, {'Post_list':Post_list})
Parent app name is mysite,child app name is polls.
I wrote in views.py
from django.shortcuts import render
from .models import Polls
def top(request):
data = Polls.objects.order_by('-created_at')
return render(request,'index.html',{'data':data})
def detail(request):
data = Polls.objects.order_by('-created_at')
return render(request,'detail.html',{'data':data})
in child app's urls.py
from django.conf.urls import url
from django.conf import settings
from django.conf.urls.static import static
from . import views
urlpatterns=[
url('top/', views.top, name='top'),
url('detail/<int:pk>/', views.top,name='detail'),
]
in parent app's urls.py
from django.contrib import admin
from django.conf.urls import url,include
app_name = 'polls'
urlpatterns = [
url('admin/', admin.site.urls),
url('polls/', include('polls.urls')),
]
in index.html
<main>
{% for item in data %}
<h2>{{ item.title }}</h2>
<a href="{% url 'polls:detail' item.pk %}">SHOW DETAIL
</a>
{% endfor %}
</main>
When I access top method, NoReverseMatch at /polls/top/
'polls' is not a registered namespace error happens.I am using Django 2.0,so I think namespace cannot used.I wrote app_name ,so I really cannot understand why this error happens.How should I fix this?What is wrong in my code?
The var app_name should be inside polls.urls not in the urls.py parent's file. Also if you want to use the new routing that Django provides remember add the path module:
from django.urls import path
path('detail/<int:pk>/', views.top,name='detail'),
If you're using the url module check this and be careful with what version you're using https://docs.djangoproject.com/en/2.0/topics/http/urls/
I've tried about a thousand different fixes suggested on different questions about this, but nothing seems to be working.
urls.py (app):
from django.contrib.auth.decorators import login_required
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', login_required(views.index), name='index'),
url(r'^clock/', views.clock, name='clock'),
]
urls.py (project):
from django.conf import settings
from django.contrib.auth import views as auth_views
from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls.static import static
from mqtt.views import auth, acl, superuser
urlpatterns = [
url('^accounts/login/', auth_views.login,
{'template_name': 'login.html'}
),
url(r'^admin/', include(admin.site.urls)),
url(r'^auth$', auth),
url(r'^superuser$', superuser),
url(r'^acl$', acl),
url(r'^$', include('lamp.urls')),
]
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
views.py:
from django.shortcuts import render
def index(request):
lamps = request.user.lamp_set.all()
context = {}
if lamps:
device_id = lamps[0].model
context = {'device_id': device_id}
return render(request, 'index.html', context)
def clock(request):
return render(request, 'clock.html')
index.html (snippet):
<div class="toggle-button" id="power"></div>
<div class="toggle-button" id="alarm-clock">
<a href="{% url 'clock' %}" id='clock-link'>Clock</a>
</div>
When I try to load the page I get:
NoReverseMatch at /
Reverse for 'clock' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
I'm pretty new to Django, so any help would be greatly appreciated.
$ means the end of urlconf search for django. you need to remove it to enable the inner-app urls search.
urlpatterns = [
url(r'^', include('lamp.urls')), # <----
#... rest of the urlconfs
]