I'm fairly new to Django and have used the tutorial videos of Sir Sentdex in youtube.
I'm trying to integrate a search function in his example but I think he used a different approach in making his example. He didn't use the app's (Product) views.py but instead went straight to url.py
url.py
urlpatterns = [ url(r'^$', ListView.as_view(queryset=Product.objects.all(), template_name="product/product.html"))]
I made a form with search in my header.html and then extends it in product.html .
Here is the code of the form with search:
<form class="navbar-form navbar-right" method='GET' action=''>
<div class="form-group">
<input class="searchfield form-control" id="searchproduct" name="q" type="text" placeholder="Search" value='{{ request.GET.q }}'>
<input type='submit' value='Search'>
</div>
</form>
How can I make the url pattern in the app's url.py like:
query = request.GET.get('q') <- I know this should be in a function.
urlpatterns = [ url(r'^$', ListView.as_view(queryset=Product.objects.filter(
Q(name__contains=query | desc__contains=query )
), template_name="product/product.html"))]
Thanks in advance.
Related
I'm new to Django.
Trying to build an app that adds two names. Pretty Basic.
Built a page that collects the names but not printing the final result.
Here is my code:
urls.py - inside the app
urlpatterns = [
path('',views.home, name='home'),
path('add',views.addname,name='add')
]
views.py
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return render(request,'input.html')
def addname(request):
val1 = (request.POST['fname'])
val2 = (request.POST['lname'])
res = 'Hi' + val1 +val2
return render(request, 'resultprint.html',{'resultprint':res})
templates/input.html
{% block content %}
<h1>Hello!</h1>
<form action='addname' method='post'>
{% csrf_token %}
Enter 1st name : <input type='text' name='fname'><br>
Enter 2nd name : <input type='text' name='lname'><br>
<input type='submit'>
</form>
{%endblock%}
templates/resultprint.html
{% block content %}
Result: {{resultprint}}
{%endblock%}
Below are the screenshots:
Couldn't really find where is the mistake happening.
I added the templates and app in the Settings file.
You have to set the same url in your urls.py :
urlpatterns = [
path('', views.home, name='home'),
path('addname', views.addname, name='addname')
]
But you can use directly the name of the url in your html file like that :
{% block content %}
<h1>Hello!</h1>
<form action='{% url 'addname' %}' method='post'>
{% csrf_token %}
Enter 1st name : <input type='text' name='fname'><br>
Enter 2nd name : <input type='text' name='lname'><br>
<input type='submit'>
</form>
{%endblock%}
Trying to have two forms on one page and have a user select a create input or load input. Should be pretty straight forward. Doesn't seem to work. Anytime I click select a person and click load, it evaluates the first URL for Create. Can someone please point out what I'm missing? I'm sure it's something simple.
Views:
def Create(request):
print('Create View')
def Load(request):
print('Load View')
URLs:
urlpatterns = [
path('', views.Index, name='index'),
path('person/', views.Create, name='Create'),
path('person/', views.Load, name='Load'),
Template:
{% block body_main %}
<form action={% url 'Create' %} method='POST'>
<h2>Name</h2>
{% csrf_token %}
{{ form1 }}
<input class="btn btn-success" name="form1btn" value="Submit" type="submit"/>
</form>
<br>
<form action={% url 'Load' %} method='POST'>
<h2>Select Existing Name</h2>
{% csrf_token %}
{{ form2 }}
<input class="btn btn-success" name="form2btn" value="Submit" type="submit"/>
<form>
{% endblock %}
Here is the problem (also mentioned in point 3 in Django URL docs:
path('person/', views.Create, name='Create'),
path('person/', views.Load, name='Load'),
Everytime django URL matcher gets here, it will usually hit Create first and return.
Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL, matching against path_info.
So it will always return the Create view.
To solve this problem I suggest creating 3 separate URLs that are more readable:
path('person/new', views.Create, name='Create'),
path('person/load', views.Load, name='Load'),
I am creating a class based view to add a new record to the db. Below is how my view looks,
class RecordCreateView(CreateView):
fields = ['name','description']
model = models.Record
success_url = reverse_lazy("index")
This is how my form looks,
<form action="{% url 'created' %}" method="post">
{% csrf_token %}
<input type="text" id="name" name="fname" placeholder="Name">
<input type="textarea" id="description" name="fdescription" placeholder="Description">
<input type="button" class="add-row" value="Add Row">
</form>
This is how my url.py file looks,
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$',views.index,name='index'),
url(r'^create/',views.RecordCreateView.as_view(),name='created'),
]
Now when I go to the admin page of django(default admin GUI) and try to add a record, I am getting the below error,
OperationalError at /admin/my_app/record/add/
no such table: abi_app_record
Any leads here will be helpful.
I'm making a blog using Django 1.9 and I've run into problem while implememting a upvote system in the website.
<form action="{% url 'blogapp:vote_handler' id=instance.get_id %}" method="POST" id="upvote_form_post">
{% csrf_token %}
<input type="hidden" name="user" value="{{request.user}}">
<input type="hidden" name="content_type_upvote" value="{{ instance.get_content_type }}">
<input type="hidden" name="object_id_upvote" value="{{ instance.get_id }}">
<button type="submit" class="icon fa-heart button-link" name="upvote_form_post" value="Submit">  {{ instance.vote_count }}</button>
</form>
This form has been giving me problems, particularly the action attribute with the url.
my url file :
urlpatterns = [
# url(r'^$', views.IndexView.as_view(), name='list'),
url(r'^$', post_list, name='list'),
url(r'^create/$', post_create),
url(r'^(?P<slug>[\w-]+)/$', views.post_detail, name='detail'),
url(r'^(?P<slug>[\w-]+)/delete/$', views.DeleteView.as_view()),
url(r'^(?P<slug>[\w-]+)/edit/$', views.UpdateView.as_view(), name='edit'),
url(r'^vote/(?P<id>[0-9]+)/$', views.vote_handler, name='vote_handler'),
]
I've no idea why this isn't working. It works fine when I hardcode the url as follows:
<form action="/vote/{{instance.id}}/" method="POST" id="upvote_form_post">
I have absolutely no idea why instance.id or instance.get_id is evaluating to a empty string.Does anyone have any idea about this?
I'm getting the error
"Reverse for 'recall' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'associate/recall/']"
When I try to submit a form. Here is my html:
<form action="{% url 'associate:recall' ordered_group %}" method="post">
{% csrf_token %}
<div>
<label for="recall">enter as many members of {{ ordered_group }} as you can recall </label>
<input type="text" id="recall" name="recall">
</div>
<div id="enter_button">
<input type="submit" value="enter" name="enter" />
</div>
<div id="done_button">
<input type="submit" value="done" name="done" />
</div>
</form>
"ordered_group" is a model object that is carried over from the 'learn' view:
urls.py:
urlpatterns = patterns('',
url(r'^learn/', "associate.views.learn", name='learn'),
url(r'^recall/', 'associate.views.recall', name='recall'),
url(r'^$', "associate.views.index", name='index'),
)
I am trying to use the ordered_group model object that is submitted in the learn view context to the html, back to the recall view as an argument. Can one do this? It makes sense to me, but what is the correct way of doing this?
views.py
def recall(request, ordered_group):
...
def learn(request):
...
ordered_group = ordered_groups[index]
return render(request, 'associate/learn.html', {'dataset':model, 'ordered_group':ordered_group})
I want to submit the form with
In you HTML, you are doing:
{% url 'associate:recall' ordered_group %}
Django expects that "recall" url is in "associate" namespace, because of the ":". But, you need to declare the namespace in urls.py, like:
url(r'^recall/', 'associate.views.recall', namespace='associate', name='recall')
If you don't want the namespace, just do:
{% url 'recall' ordered_group %}
And, about "ordered_group", you need to declare it in your url, like:
url(r'^recall/(?P<ordered_group>\w+)', 'associate.views.recall', namespace='associate', name='recall')
You are passing ordered_group in HTML, youare expecting this in views.py, but you are not expecting this on you URL.