bugthefifth
bugthefifth

Reputation: 169

Django... Fault in url path?

I am trying to build the form, which connects to the PostgreSQL database and saves information in it. The code as follows: models.py:

from django.db import models
    from django import forms
    from django.urls import reverse
    from django.forms import ModelForm

class ForQuery(models.Model):
    query_id = models.BigAutoField(primary_key=True)//I would like to have my own automatic int field
    worker_name = models.CharField(max_length=35, blank=True, null=True)
    dep_name = models.ForeignKey(Classification, models.DO_NOTHING, db_column='dep_name', blank=True, 
    null=True)

forms.py:

class ForQueryForm(forms.ModelForm):
    class Meta: 
        model=ForQuery
        fields=('work_name', 'dep_name')//only one field has to be entered while form. the other chosen.

urls.py:

urlpatterns = [
    //both paths are extensions from generic page. Which is the main page for the program
    path('', views.index, name='index'),
    path('worker_ch/', views.worker_ch, name='worker_ch'),
]

views.py:

from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import render, get_object_or_404, redirect
from scrap_site.models import ForQuery
from django.urls import reverse
from django.views.generic import TemplateView, ListView
from django.db.models import Q
from .forms import ForQueryForm

def doquery(request):
    form=ForQueryForm()
    return render(request, 'my_site/worker_ch.html', {'form':form})

worker_ch.html:

{% extends "generic.html" %}
{% load static %}
{% block css %}
  <link rel="stylesheet" type="text/css" href="{% static 'css/styles.css' %}">>
{% endblock %}
{% block content %}
<h2>Enter</h2>
<form method="post" class="for-query-form">
    {% csrf_token %}
    {{form.as_ch}}
    <input type="submit" name="" value="Submit">
</form>
{% endblock %}

this fault shows up:

File "C:\my_project\ch\ch_site\urls.py", line 22, in <module>
    path('worker_ch/', views.worker_ch, name='worker_ch'),
AttributeError: module 'ch_site.views' has no attribute 'worker_ch'

Thank you for all the help! Still studying Django and trying to figure it out. In my program, I have a fault and I've tried different ways to go around it, but it persists... And I cannot find any similar case to combat it... If possible, if you see where is my mistake, and you see something ridiculously obvious, could you please let me know what I don't understand? It feels that I don't comprehend something important about Django... It feels like each time I am fighting with Django:(, when I am doing something different and new to me... Like I am rubbing a cat the wrong way. Step to the right or left and Django, starts giving me faults after faults.. Although I am following tutorials and reading articles and book for Django.

Upvotes: 0

Views: 45

Answers (1)

ruddra
ruddra

Reputation: 51938

There are two problems I could find here. One is, there is no view named work_ch, it should be doquery:

path('worker_ch/', views.doquery, name='worker_ch'),

Two, in template, you need to replace {{form.as_ch}} with either {{ form.as_p }} or just {{ form }} should work.

Upvotes: 1

Related Questions