Django class based view giving error - django

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.

Related

Why is Django product editing not working. Reverse for 'edit' not found?

I'm trying to edit a product (without using forms.py) but I get an error Reverse for 'edit' not found. 'edit' is not a valid view function or pattern name.
vievs.py
def edit(request, id):
if (request.method == 'POST'):
obj, update = Posts.objects.update_or_create(title=request.POST.get("title"))
obj.text=request.POST.get("text")
obj.date=request.POST.get("date")
obj.image=request.POST.get("image")
obj.save()
return render(request, 'edit.html')
html
<form action="{% url "blog:edit" %}" method="post">
{% for el in posts %}
{% csrf_token %}
<input type="text" placeholder="Название" name="title" value="{{ el.title }}"><br>
<textarea placeholder="Текст статьи" rows="8" cols="80" name="text"></textarea><br>
<input type="file" name="image"><br>
<button type="submit">Добавить статью</button>
{% endfor %}
</form>
You need to define the view in your blog app's urls.py file. Something like this:
urlpatterns = [
# ... other patterns
path('<int:id>/edit/',views.edit,name='edit'),
]

Adding two numbers and show the output on the same page

I am new to Django, I am not sure if I could do action and show result on the same page.
For this page I want to add two numbers that are entered on textarea a1 and a2 together, and output it on textarea a3.
Now, the error is showing Page not found (404), I guess it is something to do with my routing.
And if I am doing dynamic content like this, should I use VueJS or react instead?
Here is my view:
def add(request):
"""The Cipher and Decipher page"""
res = 0
val1 = int(request.GET.get('a1', 0))
val2 = int(request.GET.get('a2', 0))
res = val1 + val2
return render(request, 'morse_logs/add.html', {'result': res})
my html (add.html):
{% block content %}
<h1>Addition</h1>
<form action="add">
<textarea rows="10" cols="50" name='a1' ></textarea>
<textarea rows="10" cols="50" name='a2' ></textarea>
<button type="submit" name="add">Calculate</button>
<textarea rows="10" cols="50" name="a3" >{{result}} </textarea>
</form>
{% endblock content %}
my url.py:
app_name = 'morse_logs'
urlpatterns = [
# Home Page
path('', views.index, name='index'),
# Page that shows all topics
path('topics/', views.topics, name='topics'),
# Page that do addition
path('add/', views.add, name='add'),
]
You specify in form's action attribute url where form should be send. You have add here which is not valid url. Instead you can just put "" here to show that form shoul be send to the current url:
{% block content %}
<h1>Addition</h1>
<form action="" method="get">
<textarea rows="10" cols="50" name='a1' ></textarea>
<textarea rows="10" cols="50" name='a2' ></textarea>
<button type="submit" name="add">Calculate</button>
<textarea rows="10" cols="50" name="a3" >{{result}} </textarea>
</form>
{% endblock content %}

Request.Get in url.py in django

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.

NoReverseMatch - Reverse for 'detail' arguments '()' and keyword arguments '{u'id': ''}' not found

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"> &nbsp{{ 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?

Django NoReverseMatch url issue

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.