首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AttributeError at /car/add_car 'int‘对象没有属性'get’

AttributeError at /car/add_car 'int‘对象没有属性'get’
EN

Stack Overflow用户
提问于 2021-05-11 18:12:07
回答 1查看 101关注 0票数 1

我想通过使用html输入而不是表单向我的数据库添加一个Car。但是当我运行代码时,我会得到这个错误。我对django并不熟悉,所以我无法理解人们所遇到的类似问题。

AttributeError at /car/add_car 'int‘对象没有属性'get’

我的完整跟踪输出

代码语言:javascript
复制
Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/cars/add_car

Django Version: 3.1.7
Python Version: 3.9.0
Installed Applications:
['cars.apps.CarsConfig',
 'pages.apps.PagesConfig',
 'accounts.apps.AccountsConfig',
 'contact.apps.ContactConfig',
 'houses.apps.HousesConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'ckeditor',
 'django.contrib.humanize',
 'django.contrib.sites']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "C:\Users\gx760_000\Desktop\myvenv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\gx760_000\Desktop\myvenv\lib\site-packages\django\utils\deprecation.py", line 116, in __call__
    response = self.process_response(request, response)
  File "C:\Users\gx760_000\Desktop\myvenv\lib\site-packages\django\middleware\clickjacking.py", line 26, in process_response
    if response.get('X-Frame-Options') is not None:

Exception Type: AttributeError at /cars/add_car
Exception Value: 'int' object has no attribute 'get'

My views.py

代码语言:javascript
复制
def add_car(request):
    if request.method == 'POST':
        user_id = request.POST['user_id']
        brand = request.POST['brand']
        status_of_car = request.POST['status_of_car']
        city = request.POST['city']
        ilce = request.POST['ilce']
        e_mail = request.POST['e_mail']
        phone_number = request.POST['phone_number']
        car_title = request.POST['car_title']
        description = request.POST['description']
        price = request.POST['price']
        serial = request.POST['serial']
        color = request.POST['color']
        model = request.POST['model']
        year = request.POST['year']
        condition = request.POST['condition']
        body_style = request.POST['body_style']
        engine = request.POST['engine']
        transmission = request.POST['transmission']
        interior = request.POST['interior']
        kilometers = request.POST['kilometers']
        passengers = request.POST['passengers']
        power_of_engine = request.POST['power_of_engine']
        fuel_type = request.POST['fuel_type']
        no_of_owners = request.POST['no_of_owners']

        if request.user.is_authenticated:
            user_id = request.user.id
            return user_id

        car_model = Car(user_id=user_id,
                        brand=brand,
                        status_of_car=status_of_car,
                        city=city,
                        ilce=ilce,
                        e_mail=e_mail,
                        phone_number=phone_number,
                        car_title=car_title,
                        description=description,
                        price=price,
                        serial=serial,
                        color=color,
                        model=model,
                        year=year,
                        condition=condition,
                        body_style=body_style,
                        engine=engine,
                        transmission=transmission,
                        interior=interior,
                        kilometers=kilometers,
                        passengers=passengers,
                        power_of_engine=power_of_engine,
                        fuel_type=fuel_type,
                        no_of_owners=no_of_owners)

        car_model.save()
        return HttpResponse('cars')
    return render(request, 'cars/add_car.html')

我的模板

代码语言:javascript
复制
<form action="{% url 'add_car' %}" enctype="multipart/form-data" method="POST">
    {% csrf_token %}
    {% if user.is_authenticated %}
    <input type="hidden" name="user_id" value="{{user.id}}">
    {% else %}
    <input type="hidden" name="user_id" value="0">
    {% endif %}
    <div class="featured-car content-area1">
        <div class="contact-section">
            <div class="container">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="form-section" style="height:600px; margin-bottom:0; padding: 20px 50px 0 50px;">
                            <h3>İlan Genel Bilgi</h3>
                            <div class="form-group form-box">
                                <input type="text" name="brand" class="form-control" placeholder="Marka" required>
                            </div>
                            <div class="form-group form-box">
                                <select id="status_of_car" name="status_of_car" class="form-control" required>
                                    <option>Kiralık</option>
                                    <option>Satılık</option>
                                </select>
                            </div>
                            <h3>Konum Bilgileri</h3>
                            <div class="form-group form-box">
                                <select id="city" name="city" class="form-control" required>
                                    <option>opt1</option>
                                    (...)
                                    <option>opt2</option>
                                </select>
                            </div>
                            <div class="form-group form-box">
                                <input type="text" name="ilce" class="form-control" placeholder="İlçe" required>
                            </div>
                            <h3>İletişim Bilgileri</h3>
                            <div class="form-group form-box">
                                <input type="text" name="e_mail" class="form-control" placeholder="E-Posta"
                                       {% if user.is_authenticated %} value="{{user.email}}" {% endif %} required>
                            </div>
                            <div class="form-group form-box">
                                <input type="text" name="phone_number" class="form-control" placeholder="Telefon No."
                                       {% if user.is_authenticated %} value="{{user.phone}}" {% endif %} required>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="featured-car content-area1">
        <div class="contact-section">
            <div class="container">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="form-section" style="margin-top:0; max-width:900px; height:1100px;">
                            <h3>İlan Detayları</h3>
                            <div class="form-group form-box">
                                <input type="text" name="car_title" class="form-control" placeholder="Başlık" required>
                            </div>
                            <div class="form-group form-box">
                                <script src="{% static 'ckeditor/ckeditor/ckeditor.js' %}"></script>
                                <textarea class="ckeditor" cols="80" id="description" name="description" rows="20"></textarea>
                            </div>
                            <div style="max-width:900px;">
                                <div class="form-group form-box" style="max-width:400px; margin-right:35px;">
                                    <input type="text" name="price" class="form-control" placeholder="Fiyat (TL)" required>
                                </div>
                                <div class="form-group form-box" style="max-width:400px;">
                                    <input type="text" name="serial" class="form-control" placeholder="Seri" required>
                                </div>
                            </div>
                            <div style="max-width:900px;">
                                <div class="form-group form-box" style="max-width:400px; margin-right:35px;">
                                    <input type="text" name="color" class="form-control" placeholder="Renk" required>
                                </div>
                                <div class="form-group form-box" style="max-width:400px;">
                                    <input type="text" name="model" class="form-control" placeholder="Model" required>
                                </div>
                            </div>
                            <div style="max-width:900px;">
                                <div class="form-group form-box"
                                     style="max-width:400px; max-height:100px; margin-right:35px;">
                                    <select id="year" name="year" class="form-control" required>
                                        <option>>1950</option>
                                       (...)
                                        <option>2021</option>
                                    </select>
                                </div>
                                <div class="form-group form-box" style="max-width:400px;">
                                    <input type="text" name="condition" class="form-control" placeholder="Durum" required>
                                </div>
                            </div>
                            <div style="max-width:900px;">
                                <div class="form-group form-box"
                                     style="max-width:400px; max-height:100px; margin-right:35px;">
                                    <input type="text" name="body_style" class="form-control" placeholder="Gövde Tipi" required>
                                </div>
                                <div class="form-group form-box" style="max-width:400px;">
                                    <input type="text" name="engine" class="form-control" placeholder="Motor" required>
                                </div>
                            </div>
                            <div style="max-width:900px;">
                                <div class="form-group form-box"
                                     style="max-width:400px; max-height:100px; margin-right:35px;">
                                    <input type="text" name="transmission" class="form-control" placeholder="Vites Tipi" required>
                                </div>
                                <div class="form-group form-box" style="max-width:400px;">
                                    <input type="text" name="interior" class="form-control" placeholder="İç Rengi" required>
                                </div>
                            </div>
                            <div style="max-width:900px;">
                                <div class="form-group form-box"
                                     style="max-width:400px; max-height:100px; margin-right:35px;">
                                    <input type="text" name="kilometers" class="form-control" placeholder="Kilometre" required>
                                </div>
                                <div class="form-group form-box" style="max-width:400px;">
                                    <input type="text" name="passengers" class="form-control" placeholder="Yolcu Sayısı" required>
                                </div>
                            </div>
                            <div style="max-width:900px;">
                                <div class="form-group form-box"
                                     style="max-width:400px; max-height:100px; margin-right:35px;">
                                    <input type="text" name="power_of_engine" class="form-control" placeholder="Motor Gücü" required>
                                </div>
                                <div class="form-group form-box" style="max-width:400px;">
                                    <input type="text" name="fuel_type" class="form-control" placeholder="Yakıt Tipi" required>
                                </div>
                            </div>
                            <div class="form-group form-box"
                                 style="max-width:400px; max-height:100px; margin-right:35px;">
                                <input type="text" name="no_of_owners" class="form-control" placeholder="El Değişim Sayısı" required>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="featured-car content-area1">
        <div class="contact-section">
            <div class="container">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="form-section" style="margin-top:0; max-width:600px; height:900px;">
                            <h3>Fotoğraflar</h3>
                            <div class="form-group form-box"
                                 style="max-width:500px; max-height:100px; margin-right:35px; margin-left:20px;">
                                <input type="file" name="car_photo" class="form-control" required>
                            </div>
                            <div class="form-group mb-0 clearfix"
                                 style="padding: 0 0 0 375px; padding: 15px 50px 0 50px; ">
                                <button type="submit" class="btn-md btn-theme float-left">Ekle</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

</form>
EN

回答 1

Stack Overflow用户

发布于 2021-05-11 18:34:00

你可以把这一切都处理掉

代码语言:javascript
复制
 if request.user.is_authenticated:
        user_id = request.user.id
        return user_id

代之以:

代码语言:javascript
复制
user_id = request.user.id
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67492235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档